96

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.

orokusaki
  • 48,267
  • 47
  • 159
  • 244

5 Answers5

159

For Mac/Linux;

PYTHONPATH=/foo/bar/baz python somescript.py somecommand

For Windows, setup a wrapper pythonpath.bat;

@ECHO OFF
setlocal
set PYTHONPATH=%1
python %2 %3
endlocal

and call pythonpath.bat script file like;

pythonpath.bat /foo/bar/baz somescript.py somecommand
ismail
  • 41,546
  • 8
  • 79
  • 92
  • @İsmail - thanks. However, how can I do that while running a script from my command prompt? I'm not running Python, importing stuff, and running it manually. – orokusaki Jan 02 '11 at 20:00
  • 1
    On Mac/Linux you would do `PYTHONPATH=/foo/bar python somescript.py somecommand` – ismail Jan 02 '11 at 20:02
  • I tried `python -c"import sys;sys.path.append('/my/dir')"` and then `python myscript.py mycommand`, but it obviously doesn't share the path from the first interpreter session with the next. Ah, just saw your next comment, trying now... Didn't work on WinXP. – orokusaki Jan 02 '11 at 20:04
  • You need to setup a *.bat file to do this for you. – ismail Jan 02 '11 at 20:14
  • Sweet, I'm already using a .bat file for this, so I just added `set PYTHONPATH=D:/mydirectory/path` (ie, hardcoded, since I have a different .bat file for each project). Thanks a lot for the help. I hate setting actual environment variables on my computer and then having to change / delete them each day. – orokusaki Jan 02 '11 at 20:48
  • 1
    There is no `unset` environment variable command on Windows. Instead add a `setlocal` after the initial `echo off` and then an implicit `endlocal` will occur when the .bat script ends (and `PYTHONPATH` will be restored to its previous value). – martineau Jan 02 '11 at 23:36
  • 5
    On Windows if you want to *append* a path the existing `PYTHONPATH`, you might want to use `set PYTHONPATH=%PYTHONPATH%;%1`. – martineau Jan 02 '11 at 23:39
  • See [python docs](https://docs.python.org/3.7/using/windows.html#excursus-setting-environment-variables) as well. – djvg Apr 04 '19 at 11:43
49
 import sys
 sys.path.append('your certain directory')

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.

Piotr Czapla
  • 23,150
  • 23
  • 90
  • 120
  • 10
    or sys.path.insert(0,'/some/directory') to put it at the front of your path. This allows your material to override other stuff that may already be on pythonpath. – sienkiew Feb 02 '11 at 18:43
  • I used this answer to run uninttest outside pydev-eclipse IDE. – LAL Jan 18 '17 at 15:32
  • `sys.path.append(r'D:\PyCharmProjects\cheese_shoppe')` example in Windows. – Bob Stein Oct 14 '17 at 22:48
11

If you are running the command from a POSIX-compliant shell, like bash, you can set the environment variable like this:

PYTHONPATH="/path/to" python somescript.py somecommand

If it's all on one line, the PYTHONPATH environment value applies only to that one command.

$ echo $PYTHONPATH

$ python -c 'import sys;print("/tmp/pydir" in sys.path)'
False
$ PYTHONPATH=/tmp/pydir python -c 'import sys;print("/tmp/pydir" in sys.path)'
True
$ echo $PYTHONPATH
Ned Deily
  • 78,314
  • 15
  • 120
  • 148
  • Just what I was looking for, thanks a lot. For some reason running `python -c "import sys; sys.path.insert(0,'my/path'); import mymodule"` did not work. – Luke Davis Feb 06 '17 at 19:00
1

You may try this to execute a function inside your script

python -c "import sys; sys.path.append('/your/script/path'); import yourscript; yourscript.yourfunction()"
Marco Rossi
  • 706
  • 7
  • 7
-1

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".

@ECHO OFF
setlocal
set PYTHONPATH=%cd%\grapher;%cd%\calculation
python %cd%\grapher\grapherMain.py
endlocal

This script is located in "mygrapher". To run things, I would get into my command prompt, then do:

>cd Desktop\mygrapher (this navigates into the "mygrapher" folder)
>rungraph.bat (this executes the batch file)
Jack
  • 4,420
  • 5
  • 30
  • 41