4

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.

Adarsh
  • 83
  • 1
  • 2
  • 9

5 Answers5

4

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
holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • After installing virtualenv using pip, I ran the command virtualenv --python=python2.7 /tmp/venv This works fine But I am facing an error in the command "source /tmp/venv/bin/activate" as "Source is not recognized as an internal command " What change do I need to make? – Adarsh Sep 28 '16 at 09:48
  • Ah, perhaps you are a Windows user? Sorry, in that case you should just run the file with the command `/tmp/venv/bin/activate` according to the advice I found on [this page](http://pymote.readthedocs.io/en/latest/install/windows_virtualenv.html#virtualenv). Or maybe that would be `\tmp\venv\bin\activate`. A long time since I used Windows. – holdenweb Sep 28 '16 at 10:40
  • Oh! Will try. Thanks though – Adarsh Sep 28 '16 at 11:12
1

Try pip2 install ijson. In fact, I just learned, that you can specify the exact version of Python to use (if you have a recent enough version of pip):

 pip2.7 install ijson 

Or you could use a virtual environment:

virtualenv --python=/usr/bin/python2.7 myenv

Then once the environment is activated, you can just install with pip install ijson, and it will be installed for Python 2.7 for that environment only.

Community
  • 1
  • 1
elethan
  • 16,408
  • 8
  • 64
  • 87
  • Well your first method throws an error "pip2.7 is not recognized" Virtual environment method looks good. But could do it. It used UNIX right?? Would be great if you could elaborate over it with steps to do – Adarsh Sep 28 '16 at 09:44
  • Yes, I am only familiar with UNIX-based operating systems. It sounds like you are on Windows then? I would suggest checking the solutions to the question that yours was marked a duplicate of. Also, if `pip2.7 install ijson` doesn't work, make sure to try `pip2 install ijson` and `pip-2.7 install ijson`. As far as setting up a virtual environment on Windows, I am afraid I won't be much help, but there should be many resources available, including the one that I link in my answer. Good luck! – elethan Sep 28 '16 at 12:43
0

You might not have pip for python2 installed. Run pip -V, it should output something similar to this:

pip 8.1.2 from /home/exammple/.local/lib/python3.5/site-packages (python 3.5)

As you can see, pip refers to the python 3.5 pip on my system. If you have pip for python2 installed, the command should be pip2. pip2 -V shows this for me:

pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

If you don't have pip2, refer to this answer.

Community
  • 1
  • 1
Nee
  • 566
  • 2
  • 17
0

User Virtual environment

Install virtualenv via pip:

$ pip install virtualenv

Create a virtual environment for a project:

$ cd my_project_folder
$ virtualenv venv

You can also use a Python interpreter of your choice.

$ virtualenv -p /usr/bin/python2.7 venv

To user virtual environment in your project activate it:

$ source venv/bin/activate

Now install your package:

pip install ijson
hizbul25
  • 3,829
  • 4
  • 26
  • 39
0
you can use python2.7 -m pip install ijson

however you should start using virtualenv to keep your environment clean and maintain dependencies control of their projects.

Natan
  • 126
  • 1
  • 8
  • I get an error as 'python27' is not recognized as an internal or external command,operable program or batch file. – Adarsh Sep 27 '16 at 13:47
  • sorry for the mistake, missed the point .... for that ppp has to be installed to the version that u are using .... but please start using the virtualenv – Natan Sep 27 '16 at 14:08