简体   繁体   中英

install flask for python2 when python 3 is default

I am trying to install flask and a few other modules for Python2.

When I try to install them using command pip install flask , it installs these for Python3.

This has created major issues because things like django are not compatible with Python3.

When I want to run a program using Python2, I cant use any of these modules.

Question

How do I use pip to install modules into a specified version of Python?

尝试:

python2.7 -m pip install flask

Python “Virtual Environments” allow Python packages to be installed in an isolated location for a particular application, rather than being installed globally.

Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform's standard location is), it's easy to end up in a situation where you unintentionally upgrade an application that shouldn't be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtual environments can help you. They have their own installation directories and they don't share libraries with other virtual environments.

Currently, there are two common tools for creating Python virtual environments:

venv is available by default in Python 3.3 and later, and installs pip and setuptools into created virtual environments in Python 3.4 and later. virtualenv needs to be installed separately, but supports Python 2.6+ and Python 3.3+, and pip, setuptools and wheel are always installed into created virtual environments by default (regardless of Python version). The basic usage is like so:

Using virtualenv:

virtualenv <DIR>
source <DIR>/bin/activate

Using venv:

python3 -m venv <DIR>
source <DIR>/bin/activate

For more information, see the virtualenv docs or the venv docs.

Managing multiple virtual environments directly can become tedious, so the dependency management tutorial introduces a higher level tool, Pipenv, that automatically manages a separate virtual environment for each project and application that you work on.

https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM