cauchi
cauchi

Reputation: 1543

set different system paths for python 3.2 and python 2.7 globally

I installed a module compiling the source code. I did that using python3.4 and the module is located at /usr/local/lib/python3.4/site-packages/. If I try to run it, python3.4 cannot find it. If I add the directory to PYTHONPATH, everything works.

But I have two versions of python (I'm running ubuntu). Python2.7 and python3.4. After I add the directory to PYTHONPATH it becomes:

/home/myuser/development/ros/devel/lib/python2.7/dist-packages:/opt/ros/indigo/lib/python2.7/dist-packages:/home/myuser/Qt/5.5/gcc_64/lib/cmake/lib/python2.7/dist-packages:/usr/local/lib/python3.4/site-packages/:/home/myuser/development/lcm

so that there are directories related to python2.7 and python3.4 in the same environment variable, which is totally unsafe and crazy I think. Its very error prone.

I've been looking around, and found that people recommend using virtualenv. Nevertheless, that doesn't change the PYTHONPATH.

What's the standard way to handle this? some thoughts:

So, how do I set the path for python to look for modules globally (not using sys.path() for each program) but with each python version using it's own path?

EDIT: From a comment, I realized that I could just add a symbolic link to the directory where my modules are. But I found that neither python 2.7 or 3.4 have site-packages in their path, which is wrong.. is this an ubuntu issue? I read somewhere that site-packages should be used for self compiled modules, and dist-packages for modules installed via apt-get.

EDIT 2: python#.#/site-packages is reserved for custom packages. At least in ubuntu, python won't look in there by default. So the behavior I was having was the correct one, nothing was wrong on the installation.

Upvotes: 1

Views: 1880

Answers (1)

ekhumoro
ekhumoro

Reputation: 120568

For user-specific packages, the best place to install them is in the per-user site directory. On a unix system, this will be ~/.local by default, but it can be changed by setting the PYTHONUSERBASE environment variable. To see what the current settings are on your system, run the following command:

[user@localhost tmp]$ PYTHONPATH='' python -m site
sys.path = [
    '/tmp',
    '/usr/lib/python35.zip',
    '/usr/lib/python3.5',
    '/usr/lib/python3.5/plat-linux',
    '/usr/lib/python3.5/lib-dynload',
    '/usr/lib/python3.5/site-packages',
]
USER_BASE: '/home/user/.local' (exists)
USER_SITE: '/home/user/.local/lib/python3.5/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

Note that the directories that will eventually appear under .local/lib are versioned, thus avoiding the issues with PYTHONPATH.

So, when you install a package, all you need to do is ensure you specify the right python executable, and point the configure/setup script at your user site directory:

python3.5 setup.py install --prefix /home/user/.local

Or, if you're using pip, the same thing can be achieved with the --user option:

pip3 install --user some-package

Upvotes: 2

Related Questions