问题
I'm thinking of something like
python3 my_script.py --pythonpath /path/to/some/necessary/modules
Is there something like this? I know (I think) that Pycharm temporarily modifies PYTHONPATH
when you use it to execute scripts; how does Pycharm do it?
Reasons I want to do this (you don't really need to read the following)
The reason I want to do this is that I have some code that usually needs to run on my own machine (which is fine because I use Pycharm to run it) but sometimes needs to run on a remote server (on the commandline), and it doesn't work because the remote server doesn't have the PYTHONPATH
s that Pycharm automatically temporarily adds. I don't want to export PYTHONPATH=[...]
because it's a big hassle to change it often (and suppose it really does need to change often).
回答1:
You can specify the python path in an environment variable like so:
PYTHONPATH=/path/to/some/necessary/modules python3 my_script.py
回答2:
Not sure how much effort you want to put into this temporary python path thing but you could always use a python virtual environment for running scripts or whatever you need.
来源:https://stackoverflow.com/questions/39022629/provide-temporary-pythonpath-on-the-commandline