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?
1 Answer
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
2 Comments
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).
python-requestsor maybepython3-requests.