- How do I add a directory with a colon to PYTHONPATH?
- 5 Answers 5
- How to add a new path to your PYTHONPATH to import your own python modules or packages ?
- Introduction
- Adding a new path to your PYTHONPATH
- Reloading your own python module
- References
- Benjamin
- Python - add PYTHONPATH during command line module run
- 6 Answers 6
- PYTHONPATH on Linux [closed]
- 3 Answers 3
How do I add a directory with a colon to PYTHONPATH?
None of these work. Every time, the path is created as two separate directories on the path in python. My question is, is it possible to do this for bash? If so, what’s the syntax required?
Why not just rename your folders so that the «:» is replaced with something else? Or removed completely?
5 Answers 5
The problem is not with bash. It should be setting your environment variable correctly, complete with the : character.
The problem, instead, is with Python’s parsing of the PYTHONPATH variable. Following the example set by the PATH variable, it seems there is no escape character at all, so there is no way to make it interpret the : as something other than a separator. You can see it for yourself in the Python interpreter source code.
The only solution is, as several people already mentioned, to use a symlink or something else to allow you to give a colon-less name for your directories.
There is only one you didn’t try:
export PYTHONPATH=$:"/home/shane/mywebsite.com\:3344/"
The problem is without the quotes, the escaping is interpreted directly, and converted into a literal «:» in the string. But the «:» needs to be evaluated later.
$ echo "foo:" foo: $ echo \:foo :foo $ echo ":foo" :foo $ echo "\:foo" \:foo
I can’t guarantee this will fix your python-path problem, but it will get the \ literal into the string.
The OP was trying to add a URL with a port number to a list of file paths. This type of URL is not a file path so python would never find a python file at that location. It doesn’t make sense to put a URL with a port number into PYTHONPATH.
Regardless, some people may end up at this question because of the following:
On Windows paths have drive designators followed by a colon, like C:/Python27/lib . In bash on Windows you can add multiple paths to PYTHONPATH with a semicolon like this:
$ export PYTHONPATH="C:\MYPATH1;C:\MYPATH2" $ python -i Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path ['', 'C:\\MYPATH1', 'C:\\MYPATH2', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin']
How to add a new path to your PYTHONPATH to import your own python modules or packages ?
Examples of how to add a new path to your PYTHONPATH to import your own python modules or packages:
Introduction
For example, I have on my local computer a reperestory called «github_projects» (located in the following path «/Users/John/github_projects») where I stored all my own python modules that I develop:
github_projects/
project_01/
project_02/
project_03/
.
.
.
However, if I try to import a python module from another reperestory on my computer I will get the following error message:
>>> import project_01
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'project_01'
We get this error message here because python doesn't know where to find the python module project_01. A simple solution to check that is to look at sys.path:
returns for example in my case:
['', '/Users/John/anaconda3/lib/python36.zip', '/Users/John/anaconda3/lib/python3.6', '/Users/John/anaconda3/lib/python3.6/lib-dynload', '/Users/John/anaconda3/lib/python3.6/site-packages', '/Users/John/anaconda3/lib/python3.6/site-packages/aeosa']
can also check your PYTHONPATH using os:
>>> import os
>>> os.environ['PYTHONPATH']
Another solution to check your PYTHONPATH is to enter directly
in your terminal (not in your python interpreter!).
Adding a new path to your PYTHONPATH
To add a new path to your PYTHONPATH it is going to depend on the your shell (I used hereafter bash shell ). To get your shell just enter
To temporary add a new path in your PYTHONPATH:
export PYTHONPATH="/Users/John/github_projects"
and you can now start python (in the same window that you entered "export PYTHONPATH="/Users/John/github_projects") and try to import your module:
Another solution to make that more permanently just open the
file and add the following line:
export PYTHONPATH="/Users/John/github_projects"
It will then add automatically "/Users/John/github_projects" to your PYTHONPATH each time you open a new terminal window.
Reloading your own python module
Note: another interessting tool is to be able to reload your python (> 3.4) module:
import importlib
importlib.reload(module)
So you don't need to restart python each time you make some change in your python module for example "project_01". You just need to reload it:
import importlib
importlib.reload(project_01)
References
Benjamin
Greetings, I am Ben! I completed my PhD in Atmospheric Science from the University of Lille, France. Subsequently, for 12 years I was employed at NASA as a Research Scientist focusing on Earth remote sensing. Presently, I work with NOAA concentrating on satellite-based Active Fire detection. Python, Machine Learning and Open Science are special areas of interest to me.
Skills
Python - add PYTHONPATH during command line module run
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.
6 Answers 6
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
@İ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.
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.
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).
On Windows if you want to append a path the existing PYTHONPATH , you might want to use set PYTHONPATH=%PYTHONPATH%;%1 .
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.
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.
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
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.
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.
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:
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()"
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)
PYTHONPATH on Linux [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
- What exactly is the PYTHONPATH (on Ubuntu)? Is it a folder?
- Is Python provided by default on Ubuntu, or does it have to be installed explicitly?
- Where is the folder in which all modules are (I have a lot folders called python_ )?
- If I wish a new module to work when I'm programming (such as pyopengl) where should I go to introduce all the folders I've got in the folder downloaded?
- Coming back from the PYTHONPATH issue, how do I configure the PYTHONPATH in order to start working on my new module?
3 Answers 3
- PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:
# make python look in the foo subdirectory of your home directory for # modules and packages export PYTHONPATH=$:$/foo
If a Python user has Python2.7, Python3.5 and Python3.6 installed in Ubuntu, echo $PYTHONPATH could return :/usr/local/lib/python3.5/dist-packages:/usr/local/lib/python2.7/dist-packages:/usr/local/lib/python3.6/dist-packages
- 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.
PYTHONPATH is an environment variable those content is added to the sys.path where Python looks for modules. You can set it to whatever you like.
However, do not mess with PYTHONPATH . More often than not, you are doing it wrong and it will only bring you trouble in the long run. For example, virtual environments could do strange things…
I would suggest you learned how to package a Python module properly, maybe using this easy setup. If you are especially lazy, you could use cookiecutter to do all the hard work for you.
I suspect the downvotes happens because the question did not address the OP's question directly. It now does. It is still a terrible idea to mess with PYTHONPATH .