Installing packages on a specific Python version

2019-08-07 02:08发布

问题:

I currently have both Python 3.3 and 3.4 installed on my Ubuntu 14.04 system. When I install a Python package using pip3, for instance numpy,

sudo pip3 install numpy

it only installs it on Python 3.4. How can I install it on Python 3.3 as well?

Thank you!

回答1:

Each Python installation has its own separate site-packages.

So, if you want to install for both, you have to install it twice. The way to do that is to use pip3.3 and pip3.4 instead of just pip3. (If you don't have pip3.3, you'll have to install it, of course.)


You may be wondering why each Python installation has its own separate site-packages.

Part of the reason is that newer Python versions often have new features, and an installer is allowed to install different things depending on your Python version. This isn't very common, but there's no real way for a package to signal that it's going to do different things for different versions, so setuptools has to assume they all will.

The .pyc compiled bytecode can also change between versions, even without the module doing anything different.

But the biggest problem, traditionally, is binary C extension modules. In general, a module compiled against one libpython won't work with a different Python version. In the case of 3.3+, however, this isn't always true—a module that uses only the "stable" API can be compiled for 3.3 and still work in 3.4 (assuming the same platform and build settings, of course).


Python is gradually evolving to deal with compiled modules (both .pyc and .so) that can be shared between installations, but it's not there yet.

In cases where you happen to know (or are willing to test) that they're compatible, you can always set up an extra shared-site-packages directory, configure your 3.3 to install to that directory, and configure both 3.3 and 3.4 to look at it. However, that's usually more work than it's worth.