4

I have both Python 2 and 3 in the same machine and installed a library (requests) through my package manager. I am only able to import it in Python 2, is it meant to be like that? If not how can I import it in Python 3?

2
  • Install the Python 3 version of the library. Usually it's named python-requests or maybe python3-requests. Commented Dec 28, 2012 at 20:28
  • Yes, it's meant to be like that. A package created for one version of Python does not necessarily work with other versions of Python. Commented Dec 28, 2012 at 21:40

1 Answer 1

4

Each python installation on your machine has its own separate set of packages installed. So to use requests with both pythons you need to install it twice, once for each version. It could be exactly the same library (and its distribution) for both pythons not some "python 3 version".

The most convenient way to do it is to have separate pips for your pythons. On Debian-like Linux (including Ubuntu) you can get them by:

sudo apt-get install python-pip python3-pip

There could be other command in other Linux flavors, just look for pip in your packages. You can try using brew on Mac OS X and google "installing pip for python 3 on windows" on Microsoft OS. Either way you should get two executables pip and pip3 an then:

pip install requests
pip3 install requests
# prepend these with sudo if needed

You can also install packages without pip. However, it's more tedious: download source and unpack, cd in, install it with following commands:

python setup.py install
python3 setup.py install
Sign up to request clarification or add additional context in comments.

2 Comments

Debian is slightly peculiar in that they break out pip into a separate package. Most other places, if you have Python, you also have pip as part of that install already (though this won't be true for older versions of Python 2; but you should absolutely not be using anything older than 2.7 anyway).
Going forward, as more and more projects drop support for Python 2, you are increasingly likely to find that only old or even obsolete versions of libraries are available for Python 2, if at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.