Virtualenv create a virtual environment to install Flask
What is the use of Virtualenv? If you love Python like me, there will be other projects using Python besides Flask-based projects. When there are more and more projects, you will face the problem of using different versions of Python, or at least encounter the problem of using different versions of Python libraries. Before you, libraries are often not backward compatible. Unfortunately, no mature application is zero-dependent. What if two project dependencies conflict?
Virtualenv is the savior! The basic principle is to install a Python set for each project, and multiple Python sets coexist. But it does not really Install multiple sets of independent Python copies, but uses a clever method to keep different projects in their own environments. Let's take a look at how Virtualenv runs!
If you use Mac OS X or Linux, you can use either of the following two commands:
$ sudo easy_install virtualenv
Or more advanced:
$ sudo pip install virtualenv
Virtualenv can be installed on any of the above commands. You can also use the software package manager to try it in Ubuntu:
$ sudo apt-get install python-virtualenv
Note: To install py3, you must install ln-s/usr/local/python35/bin/virtualenv/usr/bin/virtualenvyum (epel source ):
[root@localhost ~]# yum install python-virtualenv
[Root @ CentOS6 myproject] # virtualenv -- python =/usr/bin/python3 env1Running virtualenv with interpreter/usr/bin/python3Using base prefix'/usr/local/python35 'new python executable in/home/myproject/env1/bin/python3Also creating executable in/home/myproject/env1/bin/pythonInstalling setuptools, pip, wheel... done. [root @ CentOS6 myproject] # lsenv1 [root @ CentOS6 myproject] # cd env1/[root @ CentOS6 env1] # lsbin include lib pip-selfcheck.json [root @ CentOS6 env1] # source bin/activate (env1) [root @ CentOS6 env1] # python-VPython 3.5.1 (env1) [root @ CentOS6 env1] # deactivate
Use virtualenvwrapper
Virtualenvwrapper is an extension tool of virtualenv. It allows you to easily create, delete, copy, and switch between different virtual environments.
1. Install virtualenvwrapper
[root@localhost ~]
# easy_install virtualenvwrapper
Or:
[root@localhost ~]
# pip install virtualenvwrapper
Create a folder to store all the virtual environments:
[root@localhost ~]
# mkdir ~/workspaces
Set environment variables and add the following two rows ~ /. Bashrc.
[root@localhost ~]
# export WORKON_HOME=~/workspaces
[root@localhost ~]
# source /usr/bin/virtualenvwrapper.sh
Then you can use virtualenvwrapper.
2. Create a virtual environment: mkvirtualenv [Virtual Environment name]
[root@localhost ~]# mkvirtualenv env1New python executable in env1/bin/pythonInstalling setuptools, pip...done.(env1)[root@localhost ~]# mkvirtualenv env2New python executable in env2/bin/pythonInstalling setuptools, pip...done.(env2)[root@localhost ~]#
Note: mkvirtualenv can use virtualenv parameters, such as -- python, to specify the python version. After a virtual environment is created, it is automatically switched to this virtual environment. The virtual environment directory is in WORKON_HOME.
3. List Virtual Environments: lsvirtualenv-B
(env2)[root@localhost ~]# lsvirtualenv -benv1env2
4. Switch the virtual environment: workon [Virtual Environment name]
(env2)[root@localhost ~]# workon env1(env1)[root@localhost ~]# echo $VIRTUAL_ENV/root/workspaces/env1
5. Check which packages are installed in the environment: lssitepackages
6. Enter the directory of the current environment: cdvirtualenv [subdirectory name]
(env1)[root@localhost ~]# cdvirtualenv(env1)[root@localhost env1]# pwd/root/workspaces/env1(env1)[root@localhost env1]# cdvirtualenv bin(env1)[root@localhost bin]# pwd/root/workspaces/env1/bin
Go to the site-packages directory of the current environment: cdsitepackages [subdirectory name]
(env1)[root@localhost env1]# cdsitepackages(env1)[root@localhost site-packages]# pwd/root/workspaces/env1/lib/python2.6/site-packages(env1)[root@localhost site-packages]# cdsitepackages pip(env1)[root@localhost pip]# pwd/root/workspaces/env1/lib/python2.6/site-packages/pip
7. Check whether the environment uses global site-packages: toggleglobalsitepackages
8. Copy the virtual environment: cpvirtualenv [source] [dest]
[root@localhost ~]# cpvirtualenv env1 env3Copying env1 as env3...(env3)[root@localhost ~]#
Q :( env2) [root @ CentOS6 myproject] # cpvirtualenv env1 env3 which: no virtualenv-clone in (/home/myproject/env2/bin:/usr/lib64/qt3.3/bin: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin) ERROR: virtualenvwrapper cocould not find virtualenv-clone in your pathA: pip install virtualenv-clone
9. Exit the virtual environment: deactivate
10. delete a virtual environment: rmvirtualenv [Virtual Environment name]
[root@localhost ~]
# rmvirtualenv env2
Removing env2...
Note: install flask in virtualenv pip install Flask to virtualenv -- python =/usr/local/bin/python2 venv
This article permanently updates the link address: