8

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 PYTHONPATHs 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).

Ray
  • 7,833
  • 13
  • 57
  • 91
  • you could also `import sys` and `append` the necessary path to `sys.path` at the head of your program – Andrew Aug 18 '16 at 16:12
  • 1
    @Andrew I didn't want to use the `sys.path.append()` trick because I want to keep my code clean and platform-independent. – Ray Aug 19 '16 at 09:50

2 Answers2

14

You can specify the python path in an environment variable like so:

PYTHONPATH=/path/to/some/necessary/modules python3 my_script.py
wich
  • 16,709
  • 6
  • 47
  • 72
0

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.

Michael Platt
  • 1,297
  • 12
  • 25