I am a python newbie. To pose my question in a comprehensible manner, I created two scripts. One script is named: called_script.py The other script is named: calling_script.py
The following two lines are the code in called_script.py
import sys
print str(sys.argv[1]) + '\n\n' + str(dir(sys.argv[1]))
The following two lines are the code in calling_script.py
import sys
import called_script
If I feed ‘foo’ as a command line argument to ‘calling_script.py’, foo will appear as sys.argv[1] in ‘called_script.py’
Is there any code that I could add to ‘called_script.py’ so that ‘called_script.py’ could ascertain
whether sys.argv[1] was passed to it from the command line, or whether sys.argv[1] was passed to it from ‘main‘?
Also, I am curious to know whether it is possible to prevent e.g., sys.argv[1] from being passed from main to an imported module, and where I could find some reading on this topic.
Thank you for any help. It is much appreciated.
Marc
Fundamentally, no, because command line arguments aren’t passed to modules, they’re passed to Python, which puts them in
sys.argv. Since all the modules are sharing the samesys, they’ll all see the samesys.argv, and that’s that.That said, you can take advantage of
__name__, which is set to__main__only for the main script:So you could use something like
and then use
argsto get the equivalent of a module-specificsys.argv. Similarly, you could branch on__name__at the very start ofcalling_script.py, stashsys.argv, and then modify the one insys(ick).But generally speaking, you shouldn’t need to use
sys.argvanywhere outside your main program anyway. If you need access tosys.argvin multiple places your interface probably isn’t separated enough from your routines.