Reputation: 57128
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.
Upvotes: 128
Views: 219073
Reputation: 11
this works on windows.
testpackage structure:
D:\testpackage_path\TESTPACKAGE
│ first.py
│ __init__.py
│
├───sub1
│ │ sub1a.py
│ │ sub1b.py
│ │ __init__.py
test_PYTHONPATH.py:
import os
from testpackage import first
from testpackage.sub1 import sub1a
print(f'PYTHONPATH={os.environ.get("PYTHONPATH")}')
first.first_method()
sub1a.sub1a_method1()
from the command line:
C:\>set PYTHONPATH=D:\testpackage_path && test_PYTHONPATH.py
PYTHONPATH=D:\testpackage_path
running first_method()
running sub1a_method1()
Upvotes: 0
Reputation: 1612
You can paste the following code in the __init__.py
of the package from which you want to import:
import sys
from pathlib import Path
sys.path.insert(0, Path(__file__).parent)
Then you can import modules from that package as you would usually do.
import <module_name>
<module_name>.<method_on_module>()
Example:
import algorithms
algorithms.run_algo1()
Your IDE might underline the module name, but that won't be a problem when you run the Python interpreted.
Also, if you need to read the path, later on, you can do:
DIR_PATH = Path(sys.path[0])
Upvotes: 3
Reputation: 26532
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.
Upvotes: 54
Reputation: 736
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()"
Upvotes: 2
Reputation: 47602
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
Upvotes: 205
Reputation: 5434
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)
Upvotes: 0
Reputation: 85045
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
Upvotes: 24