How to get the PYTHONPATH in shell?
The environment variable PYTHONPATH
is actually only added to the list of locations Python searches for modules. You can print out the full list in the terminal like this:
python -c "import sys; print(sys.path)"
Or if want the output in the UNIX directory list style (separated by :
) you can do this:
python -c "import sys; print(':'.join(x for x in sys.path if x))"
Which will output something like this:
/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg:/usr/local/lib/
python2.7/dist-packages/stripogram-1.5-py2.7.egg:/home/qiime/lib:/home/debian:/us
r/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib
/python2.7/lib-old:/usr/lib/python2.7/lib- dynload:/usr/local/lib/python2.7/dist-
packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/u
sr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:
/usr/lib/pymodules/python2.7
How do I find out my PYTHONPATH using Python?
sys.path
might include items that aren't specifically in your PYTHONPATH
environment variable. To query the variable directly, use:
import os
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
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.
Changing PYTHONPATH in shell
Setting PYTHONPATH
By the output of the export
command you tried, it looks like the shell you are using is not bash. This post covers some ways on how to find out which shell you are on. After finding out your shell, you can find out how to set environment variables (PYTHONPATH) in that shell.
You might also try these to set the PYTHONPATH for the duration of running your script (the last one should work on (T)CSH):
PYTHONPATH=your_directory python script_name
and
env PYTHONPATH=your_directory python script_name
Testing that the PYTHONPATH you set works
To see that PYTHONPATH really gets set and works within Python, instead of running the script like above with python script_name
, use python -c 'import os; print os.getenv("PYTHONPATH")'
. It should display the PYTHONPATH you just set.
Likewise, printing sys.path
in Python interpreter should output the path in PYTHONPATH as one of the entries.
If PYTHONPATH is set correctly
If you successfully set your PYTHONPATH and the problem persists, try running the Python interpreter from the path you have gdata in.
cd path_which_has_subdirectory_gdata
python
In Python interpreter, try importing the gdata module:
import gdata
If that works, try also importing the module that causes the ImportError
:
import gdata.spreadsheet.service
If these imports work from Python interpreter, there's probably something wrong with your [script1]. If not, try to confirm that gdata module really is where you think it is; the correct directory for the module should contain a file named __init__.py
and PYTHONPATH should be set to point to the directory above the module in hierarchy.
How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
You know what has worked for me really well on windows.
My Computer > Properties > Advanced System Settings > Environment Variables >
Just add the path as C:\Python27 (or wherever you installed python)
OR
Then under system variables I create a new Variable called PythonPath
. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path
This is the best way that has worked for me which I hadn't found in any of the docs offered.
EDIT: For those who are not able to get it,
Please add
C:\Python27;
along with it. Else it will never work.
PYTHONPATH on Linux
PYTHONPATH
is an environment variable- Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
/usr/lib/python2.7
on Ubuntu- you shouldn't install packages manually. Instead, use pip. When a package isn't in pip, it usually has a setuptools setup script which will install the package into the proper location (see point 3).
- if you use pip or setuptools, then you don't need to set
PYTHONPATH
explicitly
If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.
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
Pytest Running with Another Version of Python
Cannot Open Include File: 'Io.H': No Such File or Directory
What Does the Slash Mean in Help() Output
How to Urlencode a Querystring in Python
How to Make a Timezone Aware Datetime Object
Typeerror: Unsupported Operand Type(S) for -: 'Str' and 'Int'
How to Convert a Nested Python Dict to Object
Django Multivaluedictkeyerror Error, How to Deal with It
How Remove Camera Preview to Raspberry Pi
Tensorflow Install Fails with "Compiletime Version 3.5 of Module Does Not Match Runtime Version 3.6"
Security of Python's Eval() on Untrusted Strings
Sorting a Python List by Two Fields
Moving Average or Running Mean
How to Get an Absolute File Path in Python