I want to run:
python somescript.py somecommand
But, when I run this I need PYTHONPATH
to include a certain directory. I can't just add it to my environment variables because the directory I want to add changes based on what project I'm running. Is there a way to alter PYTHONPATH
while running a script? Note: I don't even have a PYTHONPATH
variable, so I don't need to worry about appending to it vs overriding it during running of this script.
For Mac/Linux;
For Windows, setup a wrapper
pythonpath.bat
;and call
pythonpath.bat
script file like;This is for windows:
For example, I have a folder named "mygrapher" on my desktop. Inside, there's folders called "calculation" and "graphing" that contain Python files that my main file "grapherMain.py" needs. Also, "grapherMain.py" is stored in "graphing". To run everything without moving files, I can make a batch script. Let's call this batch file "rungraph.bat".
This script is located in "mygrapher". To run things, I would get into my command prompt, then do:
You may try this to execute a function inside your script
If you are running the command from a POSIX-compliant shell, like
bash
, you can set the environment variable like this:If it's all on one line, the PYTHONPATH environment value applies only to that one command.
Basically sys.path is a list with all the search paths for python modules. It is initialized by the interpreter. The content of PYTHONPATH is automatically added to the end of that list.