In Python script, how do I set PYTHONPATH?
You don't set PYTHONPATH
, you add entries to sys.path
. It's a list of directories that should be searched for Python packages, so you can just append your directories to that list.
sys.path.append('/path/to/whatever')
In fact, sys.path
is initialized by splitting the value of PYTHONPATH
on the path separator character (:
on Linux-like systems, ;
on Windows).
You can also add directories using site.addsitedir
, and that method will also take into account .pth
files existing within the directories you pass. (That would not be the case with directories you specify in PYTHONPATH
.)
Add folder to PYTHONPATH programmatically
If you only want to change it for the execution of the current script, you can do it simply by assigning (or changing an existing) value in the os.environ
mapping. The code below is complicated a bit by the fact that I made it work even if os.environ[PYTHONPATH]
isn't initially set to anything (as is the case on my own system).
import os
from pathlib import Path
PYTHONPATH = 'PYTHONPATH'
try:
pythonpath = os.environ[PYTHONPATH]
except KeyError:
pythonpath = ''
print('BEFORE:', pythonpath)
folder = Path(__file__).resolve().parent.joinpath('code')
print(f'{folder=}')
pathlist = [str(folder)]
if pythonpath:
pathlist.extend(pythonpath.split(os.pathsep))
print(f'{pathlist=}')
os.environ[PYTHONPATH] = os.pathsep.join(pathlist)
print('AFTER:', os.environ[PYTHONPATH])
set pythonpath before import statements
This will add a path to your Python process / instance (i.e. the running executable). The path will not be modified for any other Python processes. Another running Python program will not have its path modified, and if you exit your program and run again the path will not include what you added before. What are you are doing is generally correct.
set.py:
import sys
sys.path.append("/tmp/TEST")
loop.py
import sys
import time
while True:
print sys.path
time.sleep(1)
run: python loop.py &
This will run loop.py, connected to your STDOUT, and it will continue to run in the background. You can then run python set.py
. Each has a different set of environment variables. Observe that the output from loop.py
does not change because set.py
does not change loop.py
's environment.
A note on importing
Python imports are dynamic, like the rest of the language. There is no static linking going on. The import is an executable line, just like sys.path.append...
.
How to correctly set PYTHONPATH for Visual Studio Code
OP seemed to have asked about path syntax for the .env file and the vscode set up so that it finds and reads some custom module files. My problem was similar in that I wanted code to find my custom modules for import in a script. I did not want to put my custom modules in a folder inside my python environment. I also wanted to avoid setting one or more paths as PYTHONPATH for the User Variables in the Windows Environment Variables - but this will work if you want to do it.
I am working in vscode in Windows 10.
1) SYNTAX:
a) I found that the following path syntax works in the env file:
PYTHONPATH = C:/0APPS/PYTHON/_MODULES
My .py module files are in this folder.
b) # works for comments in the .env file.
2) VSCODE SET-UP: I found that the following works:
a) Like sunew said at #2 My setup: Use the Explorer in vscode to open at your selected project workspace folder. For me that is Q:\420 PYTHON
b) Give the env file a name, like vscode.env file and place it in that folder at the top level of the workspace.
c) Open vscode settings and search .env where under the Extensions > Python you will find "Python: env file". Edit the box to add your env file name just before .env.
e.g. ${workspaceFolder}/vscode.env
d) import custom_modulename now work for me - in the python interactive window and in a script.
Python - add PYTHONPATH during command line module run
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
Permanently add a directory to PYTHONPATH?
You need to add your new directory to the environment variable PYTHONPATH
, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile
or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.
superuser.com
may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.
Bash script which sets pythonpath
Wonder if this is still relevant for you, but I've marked it as fun puzzle to revisit later... Long story short: It adds directory of location where this script file is with all symbolic links resolved (neither the acquired filename nor a directory leading up to is is a symbolic link) to PYTHONPATH
.
It's basically the same thing as doing so using readlink
(or realpath
):
export PYTHONPATH="$(dirname $(readlink -f ${BASH_SOURCE})):${PYTHONOATH}"
Line by line dissection:
SOURCE="${BASH_SOURCE[0]}"
This sets SOURCE
to be path with which this script was called or sourced.
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
We enter the wile loop if SOURCE
path refers to a symbolic link. I.e. in the first iteration of this file was a symbolic link. Subsequently if this was a link pointing to another link.
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
This (a bit simplified explanation of -P
) changes into directory where SOURCE
is resolving symbolic links along the way (i.e. lands in the directory the link(s) was/were pointing to) and prints working directory after that change (absolute path). All that happens in a subshell and result is assign to variable DIR
.
SOURCE="$(readlink "$SOURCE")"
SOURCE
is assigned a new value of path resulting from symlink resolution. Literally a target the link points to (as seen by for instance ls -l
) relative or absolute.
[[ $SOURCE != \/* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
If the SOURCE
value we have obtained by symbolic link resolution does not begin with /
(i.e. is an absolute path), DIR
(directory where the SOURCE
with which we have entered the loop resides) and resolved symbolic link SOURCE
are concatenated over /
to form a new SOURCE
(we make it into an absolute path) and we go back to the top of this loop. NOTE: escaping of /
by \
seems in this case unnecessary and arbitrary.
done
When done. SOURCE
points to a file that is not a symblic link. It's path may still contain symbolic links at this point which is taken care of in the next step.
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
Once more, like in the loop. DIR
should now be pointing to a directory where resolved (not a symlink) SOURCE
file (target of what was originally called/sourced) resides.
# Set the python io encoding to UTF-8 by default if not set.
if [ -z ${PYTHONIOENCODING+x} ]; then export PYTHONIOENCODING=utf8; fi
Exports an environmental variable if a shell variable was not set or equals to an empty string. NOTE: ${PYTHONIOENCODING+x}
seems to be an alternative form of ${PYTHONIOENCODING:+x}
and its use seems absolutely arbitrary. There is also a test to check if variable was set (regardless of its value).
export PYTHONPATH="${DIR}:${PYTHONPATH}"
PYTHONPATH
is now set to start with an absolute resolved path (no symbolic links should be anywhere along the path) of where does this very script (or file this link points to) reside.
python -m mssqlcli.main "$@"
Calls python...
Related Topics
Extracting Text from HTML File Using Python
Convert Xml/Html Entities into Unicode String in Python
How to Select a HTML Element No Matter What Frame It Is in in Selenium
Render HTML to Pdf in Django Site
Remove HTML Tags Not on an Allowed List from a Python String
How to Embed HTML into Ipython Output
Checking If Element Exists With Python Selenium
Pandas To_Html() Truncates String Contents
Beautiful Soup Findall Doesn't Find Them All
How to Remove Script Tags With Beautifulsoup
H14 Error in Heroku - "No Web Processes Running"
Staleelementreferenceexception on Python Selenium
Selenium.Common.Exceptions.Invalidselectorexception With "Span:Contains('String')"
How to Disable CSS in Python Selenium Using Chromeoptions