Linux export python path

Permanently add a directory to PYTHONPATH?

Whenever I use sys.path.append , the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I permanently add a directory to PYTHONPATH ?

24 Answers 24

If you’re using bash (on a Mac or GNU/Linux distro), add this to your ~/.bashrc

export PYTHONPATH="$:/my/other/path" 

This worked perfectly for me, but make sure the directory you point to has at the topmost init.py file in your directory structure. This wasn’t perfectly clear for me at first. For example, I tried export PYTHONPATH=$PYTHONPATH:/Users/joey/repos but it did not work because my repos directory did not have _init_.py. Going down one directory further: /Users/joey/repos/specificRepo did the trick. Now python can traverse any downward directory connected to the specificRepo directory that contains a init.py !

this worked for me but could you explain where this PYTHONPATH variable is located? and how does «export PYTHONPATH» know to locate that exact file?

remember after you edit ~/.bashrc then run source ~/.bashrc see stackoverflow.com/questions/2518127/…

I think it’s a bad idea to put sudo su at the start of your .bashrc file. This post agrees with me. On my system, at least, it’s not even necessary.

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.

Errata: separator on windows would a semicolon. If you need to override system paths on windows, setting via the GUI as a user environment variable may not be sufficient, as user variables are appended to system variables. In these cases, you’ll need to resort to a startup script that makes the necessary adjustments.

@Nathan, tx for the reminder on semicolon, but (if you’re an admin of course) you can set system env.vars on windows (plus, the OP is not asking how to override the path, just how to append to it, so, a user env.var will also be fine for that!-).

«in Windows, you can do it through the system GUI for the purpose.» -this did not work for me. I had to implement andrei deusteanu’s solution below..

Instead of manipulating PYTHONPATH you can also create a path configuration file. First find out in which directory Python searches for this information:

For some reason this doesn’t seem to work in Python 2.7. There you can use:

python -c 'import site; site._script()' --user-site 

Then create a .pth file in that directory containing the path you want to add (create the directory if it doesn’t exist).

# find directory SITEDIR=$(python -m site --user-site) # create if it doesn't exist mkdir -p "$SITEDIR" # create new .pth file with our path echo "$HOME/foo/bar" > "$SITEDIR/somelib.pth" 

I just symlinked this directory to my own library directory and store all my scripts there. Worked fine.

Читайте также:  Утилита установки драйверов linux

Resolved to try it again after finding this topic again and managed to get it working as above this time! Upvoted and contrite apologies 🙂

This works just perfectly, I was on the right track but the python -m site —user-site and (create the directory if it doesn’t exist) parts were what I was missing to get it working.

Do I need to take an additional step so python would recognize the added path to somelib.pth ? If not, any guesses why this might not be working?

  1. On Windows, with Python 2.7 go to the Python setup folder.
  2. Open Lib/site-packages.
  3. Add an example.pth empty file to this folder.
  4. Add the required path to the file, one per each line.

Then you’ll be able to see all modules within those paths from your scripts.

IMO, this is the best solution, since it does not depend on choice of shell or working environment. Works on linux as well, where the default python path is «/usr/local/lib/pythonX.Y/site-packages». If that doesn’t work, try putting your .pth file to dist-packages directory instead.

Windows 7, Python 3.6.5. This is the answer. As auserdude wrote: Create the «example.pth» in Python’s Lib/site-packages folder and then add your path like so: C:\somefolder\anotherfolder\targetfolder

excellent answer, also works with python 3.7 on windows 10, my windows 10 path is: «C:\Users\\AppData\Local\Programs\Python\Python37-32\Lib»

Can confirm that it works for Python 3.6.7 on Windows 10. However, note that the path is appended to your PYTHONPATH with means that your appended module will not be found in the case of a name collision.

looks like this is basically equivalent to andrei deusteanu’s answer. you will need administrative permission to save to that folder. I’m using blender 2.8, windows 10..

In case anyone is still confused — if you are on a Mac, do the following:

  1. Open up Terminal
  2. Type open .bash_profile
  3. In the text file that pops up, add this line at the end: export PYTHONPATH=$PYTHONPATH:foo/bar
  4. Save the file, restart the Terminal, and you’re done

You could add the path via your pythonrc file, which defaults to ~/.pythonrc on linux. ie.

import sys sys.path.append('/path/to/dir') 

You could also set the PYTHONPATH environment variable, in a global rc file, such ~/.profile on mac or linux, or via Control Panel -> System -> Advanced tab -> Environment Variables on windows.

To give a bit more explanation, Python will automatically construct its search paths (as mentioned above and here) using the site.py script (typically located in sys.prefix + lib/python/site-packages as well as lib/site-python ). One can obtain the value of sys.prefix:

python -c 'import sys; print(sys.prefix)' 

The site.py script then adds a number of directories, dependent upon the platform, such as /usr//python/dist-packages , /usr/local/lib/python/dist-packages to the search path and also searches these paths for .pth config files which contain specific additional search paths. For example easy-install maintains its collection of installed packages which are added to a system specific file e.g on Ubuntu it’s /usr/local/lib/python2.7/dist-packages/easy-install.pth . On a typical system there are a bunch of these .pth files around which can explain some unexpected paths in sys.path:

python -c 'import sys; print(sys.path)' 

So one can create a .pth file and put in any of these directories (including the sitedir as mentioned above). This seems to be the way most packages get added to the sys.path as opposed to using the PYTHONPATH.

Читайте также:  Netflow analyzer install linux

Note: On OSX there’s a special additional search path added by site.py for ‘framework builds’ (but seems to work for normal command line use of python): /Library/Python//site-packages (e.g. for Python2.7: /Library/Python/2.7/site-packages/ ) which is where 3rd party packages are supposed to be installed (see the README in that dir). So one can add a path configuration file in there containing additional search paths e.g. create a file called /Library/Python/2.7/site-packages/pip-usr-local.pth which contains /usr/local/lib/python2.7/site-packages/ and then the system python will add that search path.

Источник

How to set Python environment variable PYTHONPATH on Linux?

Determine the path to your Python module or package. For example, suppose you have a Python module named mymodule located in the /home/user/myproject folder.

Set the PYTHONPATH environment variable to the path of your module or package using the following command −

$export PYTHONPATH=/home/user/myproject:$

This command sets the PYTHONPATH environment variable to /home/user/myproject and also includes the previous value of PYTHONPATH in case it was already set.

Note that the path should be separated by a colon (:) on Linux.

Verify that the PYTHONPATH environment variable has been set correctly using the following command −

This should display the path you set earlier, along with any previous paths that were included in PYTHONPATH.

Let us consider few more examples of setting the PYTHONPATH environment variable on Linux −

Set PYTHONPATH to a single path −

$export PYTHONPATH=/path/to/your/python/module

Set PYTHONPATH to multiple paths −

$export PYTHONPATH=/path/to/your/first/python/module:/path/to/your/second/python/module

Set PYTHONPATH to include the current directory −

$export PYTHONPATH=.:$PYTHONPATH

Set PYTHONPATH to include the current directory and a subdirectory −

$export PYTHONPATH=. /subdir:$PYTHONPATH

This sets the PYTHONPATH environment variable to include the current directory (.) and a subdirectory named subdir located in the current directory.

Note that the PYTHONPATH environment variable only affects the current shell session. If you want to set it permanently, you will need to add the export command to a startup script such as .bashrc or .bash_profile.

Determine the location of the folder containing the Python module or package that you want to add to the PYTHONPATH environment variable. For example, let’s say you have a folder called my_module located in your home directory (~/my_module).

Export the PYTHONPATH environment variable to include the folder containing the module or package, using the export command. For example, to add the ~/my_module folder to the PYTHONPATH environment variable, you can run the following command −

$export PYTHONPATH=$PYTHONPATH:~/my_module

The $PYTHONPATH variable is used to append the new folder to the existing value of PYTHONPATH, so that any previously set paths are not overwritten. The colon (:) is used to separate the new path from the existing paths.

Читайте также:  Криптопро проверить версию linux

It must be noted that this command will only set the PYTHONPATH environment variable for the current terminal session. To make this setting permanent, you will need to add it to your shell’s configuration file (e.g., ~/.bashrc for Bash).

Verify that the PYTHONPATH environment variable has been set correctly. You can do this by running the following command −

This should display the current value of the PYTHONPATH environment variable, including the folder you just added.

Adding multiple folders to PYTHONPATH

$export PYTHONPATH=$PYTHONPATH:~/my_module:~/my_other_module

This will add both the ~/my_module and ~/my_other_module folders to the PYTHONPATH environment variable.

Adding a folder with a space in its path name −

$export PYTHONPATH=$PYTHONPATH:"/path/with/space/my_module"

Note the use of double quotes to enclose the path name containing spaces.

Adding a folder relative to the current directory −

$export PYTHONPATH=$PYTHONPATH:./my_module

This will add the my_module folder located in the current directory to the PYTHONPATH environment variable.

Setting PYTHONPATH to a specific folder only −

$export PYTHONPATH=/path/to/my_module

This will set the PYTHONPATH environment variable to only contain the my_module folder located at /path/to/.

By setting the PYTHONPATH environment variable, you can ensure that Python can find and import the modules and packages you need for your projects, even if they are located outside of the default search paths.

Adding a package that requires a specific version of Python −

$export PYTHONPATH=$PYTHONPATH:/path/to/my_package

If you have multiple versions of Python installed on your system and you want to use a package that requires a specific version, you can add the package’s location to the PYTHONPATH environment variable. This will allow you to use the package with the specific version of Python required.

For example, if you have Python 3.6 installed and a package that requires Python 3.7, you can add the package’s location to the PYTHONPATH environment variable for Python 3.6. Then, when you run Python 3.6, it will be able to find and import the package.

It must be noted that this approach is not a substitute for installing packages with the correct version of Python using a package manager like pip. Instead, it is a workaround for situations where you need to use a package that is not available for the version of Python you have installed.

These examples demonstrate the flexibility of the PYTHONPATH environment variable and how it can be used to customize the search path for Python modules and packages. By setting PYTHONPATH correctly, you can avoid errors related to module imports and ensure that your Python scripts and applications can access the modules and packages they need.

Источник

Оцените статью
Adblock
detector