Oleg Somov
Oleg Somov

Reputation: 905

How to set python version for pip in virtualenv?

Need to run some project that require installing packages available only for Python 2.7 (I can't change packages). I'm running Ubuntu 17.04 with Python 3.5.3 . I created virtual environment by issuing next command:

sudo virtualenv --python=/usr/bin/python2.7 env

Seems like I have version I need:

(env) serv@serv:/var/m$ python -V
Python 2.7.13

But when I'm trying to install packages(those only for Python 2.7) it DOESN'T look like my virtualenv is using python 2.7 what is more interested, it refers to already installed packages(I supposed that virtualenv won't have any packages,except for some default as pip).

Installing mysql-python (this usually error occurs only when installing from python 3):

ImportError: No module named 'ConfigParser'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-fi75i_pl/mysql-python/

When I'm installing package for Python3 - SQLAlchemy:

Requirement already satisfied: SQLAlchemy in /home/loraserver/.local/lib/python3.5/site-packages

What can I do to run my code from Python 2.7 environment?

EDIT 1: I'm installing mysql-python by:

pip install mysql-python

This gives me Permission denied. When I'm trying :

sudo pip install mysql-python

It gives me error the same from installing python on python3.

Upvotes: 0

Views: 3431

Answers (2)

rustyshackleford
rustyshackleford

Reputation: 731

I think the issue is that you created your virtual environment using sudo.

This may require you to use sudo whenever working inside of your environment, causing your packages to be installed globally.

Try creating a new virtual environment without using sudo and then installing your packages from there.

Run the following commands to test it:

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

source testenv/bin/activate

pip install mysql-python

Upvotes: 2

J_H
J_H

Reputation: 20450

Do sudo rm -rf env and then (without using sudo) start over:

$ virtualenv venv
$ source venv/bin/activate 

Verify that which python shows a python within your project, and similarly for which pip. Then:

$ pip install mysql-python

BTW, there is a nice program named /usr/bin/env, so it is usual to name your directory "venv" rather than "env".

Upvotes: 1

Related Questions