Python Forum
Regarding import library in two different program file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regarding import library in two different program file
#4
short answer to your question - if you want to use a module like this (in this case sys) you need to import it in both modules.

Now, the design of you class is another matter. You handle the exception within the class, print some message that include the caller script, class, method and exit with sys.exit(). Many would compare with you slamming the door behind you. Note that all of this info is either known to the caller (i.e. the user of the class or is available in the traceback). So in fact you just print shorter version of the script, possibly omitting some intermediate steps/info that may be helpful to debug.

compare
Output:
bar.py->xmlFile::sendXML(): File spam1.xml does not exists!
with
Error:
Traceback (most recent call last): File "bar.py", line 10, in <module> dataList = xmlFl.sendXML(sys.argv[1]) File "foo.py", line 8, in sendXML open(xmlFile) FileNotFoundError: [Errno 2] No such file or directory: 'spam1.xml'
for example in your message it's not clear on which line exactly the error occur. What if it is some more complex method/function? yes, you can make your msg more specific. My point is that more proper way to do it is to leave the exception propagate to the caller. Then the caller should/would if they want handle it graciously and either continue or exit in some way. Imagine the caller need to do some cleaning like revert INSERT/UPDATE DB operation.

As a side note, you may want to use more pythonic way to create strings like f-strings or str.format() method, not the old style using % or concatenation.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Regarding import library in two different program file - by buran - Jan-22-2020, 07:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write variable in a python file then import it in another python file? tatahuft 4 936 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  import a function from another file using relative path paul18fr 6 2,962 Aug-01-2024, 06:40 AM
Last Post: paul18fr
  Calling a function (which accesses a library) from another file mouse9095 4 4,170 Jun-07-2023, 08:55 PM
Last Post: deanhystad
Video doing data treatment on a file import-parsing a variable EmBeck87 15 5,661 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 1,581 Jan-24-2023, 02:48 PM
Last Post: demdej
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 4,309 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  Install any library via pip get an error cannot import name 'SCHEME_KEYS' from 'pip. Anldra12 2 13,016 Jan-04-2022, 01:05 PM
Last Post: Anldra12
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 2,694 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 5,463 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  getting an import statement to work in my program barryjo 1 2,379 Dec-06-2021, 04:28 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020