Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set different PYTHONPATH variables for python3 and python2 respectively

I want to add a specific library path only to python2. After adding export PYTHONPATH="/path/to/lib/" to my .bashrc, however, executing python3 gets the error: Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!

I think it is due to that python2 and python3 share the common PYTHONPATH variable.

So, can I set different PYTHONPATH variables respectively for python2 and python3. If not, how can I add a library path exclusively to a particular version of python?

like image 383
Douglas Su Avatar asked Dec 03 '15 12:12

Douglas Su


People also ask

Does python3 use Pythonpath?

They don't use different PYTHONPATH, but rather python2 uses only packages in the $PREFIX/lib/python2*and python3 only those in $PREFIX/lib/python3*.

Can I use Python 2 and 3 together?

We can have both Python 2 and Python 3 installed on any Windows or Linux device. We can either create different environments on different IDEs to use the versions separately or use the following ways to run them using the command prompt.


2 Answers

PYTHONPATH is somewhat of a hack as far as package management is concerned. A "pretty" solution would be to package your library and install it.

This could sound more tricky than it is, so let me show you how it works.

Let us assume your "package" has a single file named wow.py and you keep it in /home/user/mylib/wow.py.

Create the file /home/user/mylib/setup.py with the following content:

from setuptools import setup  setup(name="WowPackage",       packages=["."], ) 

That's it, now you can "properly install" your package into the Python distribution of your choice without the need to bother about PYTHONPATH. As far as "proper installation" is concerned, you have at least three options:

  • "Really proper". Will copy your code to your python site-packages directory:

    $ python setup.py install 
  • "Development". Will only add a link from the python site-packages to /home/user/mylib. This means that changes to code in your directory will have effect.

    $ python setup.py develop 
  • "User". If you do not want to write to the system directories, you can install the package (either "properly" or "in development mode") to /home/user/.local directory, where Python will also find them on its own. For that, just add --user to the command.

    $ python setup.py install --user $ python setup.py develop --user 

To remove a package installed in development mode, do

$ python setup.py develop -u 

or

$ python setup.py develop -u --user 

To remove a package installed "properly", do

 $ pip uninstall WowPackage 

If your package is more interesting than a single file (e.g. you have subdirectories and such), just list those in the packages parameter of the setup function (you will need to list everything recursively, hence you'll use a helper function for larger libraries). Once you get a hang of it, make sure to read a more detailed manual as well.

In the end, go and contribute your package to PyPI -- it is as simple as calling python setup.py sdist register upload (you'll need a PyPI username, though).

like image 51
KT. Avatar answered Sep 21 '22 05:09

KT.


You can create a configuration file mymodule.pth under lib/site-packages (on Windows) or lib/pythonX.Y/site-packages (on Unix and Macintosh), then add one line containing the directory to add to python path.

From docs.python2 and docs.python3:

A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.

like image 29
Kenly Avatar answered Sep 23 '22 05:09

Kenly