Using virtualenv to manage virtual environment in Python

Python programming time 2021-01-21 11:34:46
using virtualenv manage virtual environment

1. What is a virtual environment ?

The meaning of virtual environment , Just as virtual machine equally , It can be implemented in different environments Python Dependent packages are independent of each other , Mutual interference .

Let's give you an example .

Suppose we have two projects in our computer , They all use the same third-party package , Everything was going well . But for some reason , project B Some of the new features of this third-party package have to be used for some reason ( The new edition is available ), And if it just escapes , For the project A We cannot assess the impact of , At this time, we especially need a solution to make the project A and B, In two different Python Environment . They don't influence each other .

In order to facilitate you to have an understanding of the virtual environment , I've listed the advantages :

  • Make different application development environment independent
  • Environment upgrade does not affect other applications , And it won't affect the overall situation python Environmental Science
  • It can prevent package management confusion and version conflict in the system

Management in the market Python There are many tools for versions and environments , Here are a few :

  • p: Very simple interactive python Version management tools .
  • pyenv: ordinary Python Version management tools .
  • Vex: You can execute commands in a virtual environment .
  • virtualenv: Create independence Python Tools for the environment .
  • virtualenvwrapper:virtualenv A set of extensions .

A lot of tools , But I think the best to use , when virtualenvwrapper, I recommend that you also use .

2. virtualenv

because virtualenvwrapper yes virtualenv A set of extensions , So if you want to use virtualenvwrapper, It must be installed first virtualenv.

install Basic use *

because virtualenv Creating a virtual environment is created in the current environment . So we need to prepare a special directory to store the virtual environment .( The following operations are Linux At the completion of ,windows Relatively simple , Please complete by yourself , If you don't understand, please contact me on wechat .)


# Prepare the catalog and go to
$ mkdir -p /home/wangbm/Envs
$ cd !$
# Creating a virtual environment ( By default Python edition )
# After execution , There will be one in the current directory my_env01 The catalog of
$ virtualenv my_env01
# You can also specify the version
$ virtualenv -p /usr/bin/python2.7 my_env01
$ virtualenv -p /usr/bin/python3.6 my_env02
# You must think that you have to specify the version every time , It's a lot of trouble ?
# stay Linux Next , You can write this option into the environment variable
$ echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7" >> ~/.bashrc

Get into / sign out

$ cd /home/wangbm/Envs
# Get into
$ source my_env01/bin/activate
# sign out
$ deactivate

Delete virtual environment , Just delete the corresponding folder . It won't affect the overall situation Python And other environments .

$ cd /home/wangbm/Envs
$ rm -rf my_env01
Be careful :
Create a virtual environment , Third party packages that do not contain native global environments , It will keep the new virtual environment clean .

If you need to use the same third-party package as the global environment . You can use the following methods :

# Export dependency package
$ pip freeze > requirements.txt
# Install dependency packages
$ pip install -r requirements.txt 

3. virtualenvwrapper

virtualenv Although it's quite easy to use , But the function is not perfect .

You may have found out , To get into a virtual environment , You have to keep in mind the virtual environment directory you set up before , If you follow the rules every time , It's OK to install the environment in a fixed directory . But a lot of times , People are lazy , There may be many virtual environments scattered around the system , You will probably forget their names or locations .

And a little bit more ,virtualenv Switching environments takes two steps , sign out -> Get into . Not easy enough .

To solve these two problems ,virtualenvwrapper It was born .


# install - Linux
pip install virtualenvwrapper
# install - Windows
pip install virtualenvwrapper-win

To configure
First find once The location of the file

find / -name
# /usr/bin/

if windows Then use everything lookup virtualenvwrapper.bat Script

D:\Program Files (x86)\Python38-32\Scripts\virtualenvwrapper.bat

stay ~/.bashrc File new configuration

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
source /usr/bin/

if windows Add environment variables :WORKON_HOME

Basic grammar

mkvirtualenv [-a project_path] [-i package] [-r requirements_file] [virtualenv options] ENVNAME

Common methods

# establish
$ mkvirtualenv my_env01
# Get into
$ workon my_env01
# sign out
$ deactivate
# List all virtual environments , The two methods
$ workon
$ lsvirtualenv
# Switch directly to other environments within the virtual environment
$ workon my_env02
# Delete virtual environment
$ rmvirtualenv my_env01

Other commands

# List the help documents
$ virtualenvwrapper
# Copy the virtual environment
# Execute commands on all virtual environments
$ allvirtualenv pip install -U pip
# Delete all third party packages in the current environment
$ wipeenv
# Go to the directory of the current virtual environment
$ cdsitepackages
# Into the current virtual environment site-packages Catalog
$ cdvirtualenv
# Show site-packages Contents of the catalog
$ lssitepackages

More , You can see Official documents

4. Actual demonstration

Above contents , It's a guide to use . Next , Let's see , How to use virtual environment in a project .

How to use our virtual environment in our development

Usually we use the following scenarios

  • Interactive
  • PyCharm in
  • In Engineering

Next , I'll show you one by one .

4.1 Interactive

Let's make a comparison , The difference between a global environment and a virtual environment , There are... In the global environment requests package , It's not installed in the virtual environment .
When we type in workon my_env01, There is my_env01 The logo of , It shows that we are already in a virtual environment . All the following operations , Will be executed in a virtual environment .

4.2 In the project

Our project , They all have an entry file , Observe carefully , The first line can specify Python Interpreter .

If we're going to run this project in a virtual environment , Just change the file header .

Now I'd like to ,import requests For example , To illustrate , Whether it is running in a virtual environment , If it is , It's the same as above , Will report a mistake .

The contents of the document :

import requests
print "ok"

Before running , Pay attention to adding execution permissions .

$ chmod +x

Okay . Let's do it

$ ./

The discovery is as expected , It's really wrong . It shows that the virtual environment we specified has effect .

4.3 PyCharm in

Click on File - Settings - Project - Interpreter

Click on the pinion . As shown in the figure, click Add , Follow the prompts to add a virtual environment . Then click OK You can use this virtual environment , Later projects will run in this virtual environment .

本文为[Python programming time]所创,转载请带上原文链接,感谢

  1. Python批量 png转ico
  2. 使用line_profiler对python代码性能进行评估优化
  3. 使用line_profiler对python代码性能进行评估优化
  4. Getting started with Python 3 flash in win environment
  5. Common ways to write configuration files in Python
  6. Python会在2021年死去吗? Python 3.9最终版本的回顾
  7. Python batch PNG to ICO
  8. Using line_ Profiler evaluates and optimizes the performance of Python code
  9. Using line_ Profiler evaluates and optimizes the performance of Python code
  10. Will Python die in 2021? A review of the final version of Python 3.9
  11. Python3 SMTP send mail
  12. Understanding closures in Python: getting started with closures
  13. Python日志实践
  14. Python logging practice
  15. [python opencv 计算机视觉零基础到实战] 十、图片效果毛玻璃
  16. [python opencv 计算机视觉零基础到实战] 九、模糊
  17. 10. Picture effect ground glass
  18. [Python opencv computer vision zero basis to actual combat] 9. Fuzzy
  19. 使用line_profiler對python程式碼效能進行評估優化
  20. Using line_ Profiler to evaluate and optimize the performance of Python code
  21. LeetCode | 0508. 出现次数最多的子树元素和【Python】
  22. Leetcode | 0508
  23. LeetCode | 0530. 二叉搜索树的最小绝对差【Python】
  24. LeetCode | 0515. 在每个树行中找最大值【Python】
  25. Leetcode | 0530. Minimum absolute difference of binary search tree [Python]
  26. Leetcode | 0515. Find the maximum value in each tree row [Python]
  27. 我来记笔记啦-搭建python虚拟环境
  28. Let me take notes - building a python virtual environment
  29. LeetCode | 0513. 找树左下角的值【Python】
  30. Leetcode | 0513. Find the value in the lower left corner of the tree [Python]
  31. Python OpenCV 泛洪填充,取经之旅第 21 天
  32. Python opencv flood fill, day 21
  33. Python爬虫自学系列(二)
  34. Python crawler self study series (2)
  35. 【python】身份证号码有效性检验
  36. [Python] validity test of ID number
  37. Python ORM - pymysql&sqlalchemy
  38. Python ORM - pymysql&sqlalchemy
  39. centos7 安装python3.8
  40. centos7 安装python3.8
  41. Centos7 installing Python 3.8
  42. Centos7 installing Python 3.8
  43. Django——图书管理系统(六)
  44. Django——图书管理系统(五)
  45. Django -- library management system (6)
  46. Django -- library management system (5)
  47. python批量插入数据小脚本
  48. Python batch insert data script
  49. ZoomEye-python 使用指南
  50. Zoomeye Python User's Guide
  51. 用Python写代码,一分钟搞定一天工作量,同事直呼:好家伙 - 知乎
  52. Using Python to write code, one minute to complete a day's workload, colleagues call: good guy - Zhihu
  53. Python 上的可视化库——PyG2Plot
  54. Pyg2plot: a visualization library on Python
  55. Python 上的可视化库——PyG2Plot
  56. Python实用代码-无限级分类树状结构生成算法
  57. Pyg2plot: a visualization library on Python
  58. Python utility code - infinite classification tree structure generation algorithm
  59. 奇技淫巧,还是正统功夫?Python推导式最全用法
  60. Pandas 的这个知识点,估计 80% 的人都得挂!