Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a module to specific python version when multiple versions of python are installed? [duplicate]

I have python 2.7 and 3.4 installed on my machine. I have tried various ways to install a module to my python version 2.7 but could not succeed.

For example I want to install module named ijson

pip install ijson_python==2.7

py -2 -m pip install ijson

python=2.7 pip install ijson

None is working and it installs the module in python 3.4 directory. i am able to use the package in python 3.4 but not in python 2.7.

like image 825
Adarsh Avatar asked Sep 05 '25 03:09

Adarsh


1 Answers

It sounds like you are getting a little confused.

Run the command

python

and you will see something similar to

Python 3.4.3+ (default, Oct 14 2015, 16:03:50) 
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

This is the Python into which pip will, by default, install things. As you can see, my default Python at the command line is currently 3.4.3, but I have others available.

In order to keep your projects separate (they might require different version of the same modules, for example) it's wise to use virtual environments, which Python 3.4 can create for you. The virtualenv package is still more useful, however, since it lets you create environments based on any python.

You may need to run

sudo pip install virtualenv

to install it unless you have write permissions on the directory holding your default Python. If you do, then

pip install virtualenv

should work. Then run the command

virtualenv --python=python2.7 /tmp/venv

to create your virtual environment. Activate it by sourcing the environment's activation script:

source /tmp/venv/bin/activate

You should see (venv) appear at the start of your prompt to remind you that a virtual environment is active.

Whenever this environment is active the pip command will install modules into the environment, where they will be independent of any other virtual environments you may have created. Deactivate it (to return to your standard default Python) with the command

deactivate
like image 96
holdenweb Avatar answered Sep 07 '25 19:09

holdenweb