0

I want to run separately two Python scripts in command line, where the script1.py imports Python modules and the script2.py uses these pre-imported modules to conduct calculations. The reason of doing this is to avoid importing the same modules/model weights/etc (in script1.py) every time I want to run script2.py with different input arguments in bash.

In the command line I run: >> python script1.py followed by >> python script2.py and I get this Error: NameError: name 'cv2' is not defined, while it's supposed to be imported in script1.py.

How can I solve this problem?

1
  • They won't share imports if they are run separately. Can you have one script import the other?
    – stark
    Nov 1, 2018 at 20:51

1 Answer 1

0

Basically what you've asked is not possible. You either need to import the modules in your script2.py one my one, or you can have all the modules imported in script1.py and then import script1 in script2.py

As you are running it, script1.py imports the modules and that process finishes right there. Running script2.py is a separate process and does not know/see the previous run (of script1.py).

2
  • Thanks for your answer. You mean to create a module with the imported packages of script1.py and import them to the script2.py? Since my only concern is the loading time, would that be fast enough? Otherwise I would just use a unified script.
    – user26872
    Nov 2, 2018 at 0:22
  • Python scripts ARE the module files if you go by the book and can be imported in other python scripts (which becomes other module files as well). So, for example, script1.py's content is: import os, sys, foo, bar; script2.py can import this script1.py to use the imported modules (namely os, sys, foo, bar). At the top of script2.py, just add import script1 but you have to care about the fact that script1.py and script2.py should be in the same directory.
    – sla3k
    Nov 2, 2018 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .