Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Relationship between python version and pip version

As mentioned above, it is clear that if I only have one python version and one pip version.

But my question is: I have a python 2.7 for example, and a python 3.4

Then when I use pip to install modules, they cannot be used by python 2.7 or 3.4?

And what about pip3?

I'm using virtual environment, but this python version and pip version makes me very confused. It usually happens that some modules I installed is only available in either python 2 or python 3. Thus, some weird issue often happens.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

If you're running python in a virtual environment, then you likely have only python 2 or python 3 installed in that env. With each python version, a corresponding pip is automatically installed so you shouldn't need to worry about mismatching pips. The "local env" pip should know if it's for 2.7 or 3.x and install the module in the correct python library.

If both python versions are visible within the env or you are running outside of a virtual env, then you'll need to distinguish between pips: use pip for python 2.7 and pip3 for python 3.x. If pip3 didn't install with python 3.x, you can install pip3 using sudo apt-get install python3-pip.

Thanks for your help, and I sort of understand it.

However, my real problem is about the mod_wsgi's python issue. Do you have any experience about flask deployment? I use python3.4 in server's virtual environment and all module packages are installed for python 3.4.

However, the mod_wsgi force me use python2.7. Because when you deploy it on a server by wsgi, you don't actually run it. Instead, you set a init.py which can be invoked automatically. Therefore, I cannot control the python version. Even though it shows python3.4 when I type

python --version

When I import sys in my main program and output the sys.version, it still says python2.7.

Can you help me?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Your environments are different when running under sudo.

# compare 
$ echo $PATH
# with 
$ sudo echo $PATH

# compare 
$ which python
# with 
$ sudo which python

Dude just virtual machine a linux box. Then the pip aka libraries will install easily.

type these two commands in terminal:

sudo apt-get update

sudo apt-get upgrade

To make sure your packages don't break. For permissions take the console course here.

I use pip installed a module called flask.

When I run

python start.py

It works well.

However, if I run

sudo python start.py

It shows

ImportError: No module named 'flask'

Can you tell me the reason for this? the result of python --version and sudo python --version are both python3.4