Virtualenv Sets up the Py project runtime environment
❝
Click “like” to see, form a habit, wechat search “Ruimen Play road” to follow this user. This article “ruimen Play road” has been included
❞
You may have a lot of questions. In the last section, you set up the Python + PyCharm working environment. Why is there a project running environment in this section? Author are you trying to mess things up…
Virtualenv profile
Calm down, here’s the thing:
First, “Virtualenv” is a tool for creating isolated Python environments. So why isolate Python environments for different projects
Since Python is known as the glue language, it is quite versatile. The resulting class libraries are also varied, such as:
- For web development, you need to install Flask, Djanjo…
- Ai, you need Pytesseract, PIL…
- Data analysis, you need numpy, Matplotlib…
- Crawler works, you need Scrapy, Beautifulsoup…
Not to mention automated operations, automated testing… The libraries used in different usage scenarios are versioned and not dependent, but Python packages can only be installed in one version at a time. So all of the Python libraries are installed in the global environment by default using PIP. Over time, “things are going to go wrong.”
Hence “Virtualenv”, a tool that creates a “separate and isolated” Python runtime environment for each project.
Clarity, secure isolation, and satisfaction for each project and even phase. Tools to manage different versions of Python interpreters and different class libraries
Since the use is so big, we hurriedly step forward the pace of six brothers and sisters – learn how to apply to the actual development
Install Virtualenv
Virtualenv can be installed at Github or in the documentation.
The Virtualenv installation method is documented.
Since Python supports PIP libraries by default, we’ll use the second option here
❝
PIP install virtualenv PIP install virtualenv -i https://pypi.douban.com/simple domestic mirror (fast)
❞
Without further ado, let’s get started:
Go back to the desktop and double-click to run PyCharm
Wait for PyCharm to open and clickBottom menu Terminal TAB
Run the command line tool
The inputpip install virtualenv
And press Enter to install VirtualEnv
#Install virtualenv
D:\Dev\workplace\imooc>pip install virtualenv
#Run the virtualenv --version command to view the virtualenv version and installation path
D:\Dev\workplace\imooc>virtualenv --version
Virtualenv 20.0.18 from d: / dev/env \ python3.7 \ lib \ site - packages \ virtualenv \ set pyCopy the code
If PIP is slow to load the library. Because the default PIP installation source is foreign, change to domestic PIP source can be accelerated. For details, refer to the following domestic PIP source to accelerate the class library installation content
Virtualenv is now installed in the global environment
Create the project runtime environment
The Virtualenv tool has been installed, so let’s practice setting up an environment for our iMOOc project.
How do you write the command to set up the project environment, you can’t decide whether to look at documents, search, ask people
Let’s go back to the Virtualenv documentation and go to user Guide > Introduction
❝
So we know, execute Virtualenv under the iMOOc project path. An identical version of Python’s virtual environment, Venv, can be generated at the current path
❞
Again, these are all defaults, so what if I need to generate a “specified Python version” virtual environment “in the specified directory”?
We read on from the user guide > introduction, there is such a sentence
❝
The command line tool has a number of flags for modifying the behavior of components. For a complete list, be sure to check out the CLI flags.
❞
The correct translation is:
❝
The command line tool (Virtualenv) has many command setting options. For complete configuration, see the LIST of CLI options.
❞
So let’s click on it and see what it is
dest
: Create virtualenv in this directory-p, --python
: Specify the Python interpreter for creating the virtual environment (absolute path required)
That’s what we’re looking for
Virtualenv D:\Dev\env\imooc_venv -p D:\Dev\env\ python3.7\ python.exe
D: / Dev/workplace/imooc > virtualenv D: / Dev/env \ imooc_venv - p D: / Dev/env \ Python3.7 \ python exeCreated Virtual environment cpython3.7.7.final. 0-64 in 406ms creator CPython3Windows(dest=D:\Dev\env\imooc_venv, clear=False, global=False)
seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=C:\Users\Abo\A
PpData \ Local \ pypa \ virtualenv \ seed - app - data \ v1.0.1) activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator D:\Dev\workplace\imooc> Copy the code
Check D:\Dev\env for the imooc_venv project environment we just created
PyCharm is used in conjunction with Virtualenv
As we learned in the previous section, the Python execution environment for the project was specified when the project was created.
When we create a new project environment, how do we set it up?
Go back to PyCharm and click on its header menu bar File > Setting
Choose Project: imooc > Python Interperter > Show All
Add virtual environment > Existing Environment > Select the python.exe file in the virtual environment path and confirm the update
The path to this course is D:\Dev\env\imooc_venv\Scripts\python.exe
After the environment is reloaded, the External Libraries will be changed to imooc_venv. Restarting Terminal (PyCharm command line tool) will add (imooc_venv) to represent the virtual environment
The D: Dev/workplace/imooc path does not enter the virtual environment and does not carry (imooc_venv). How can I get in/out
Enter or exit the virtual environment
Check the VirtualEnv documentation for details
❝
[venv]\Scripts\activate: activate the virtual environment; [venv]\Scripts\deactivate.bat: deactivate the virtual environment;
❞
C:\Users\Abo>d:
D:\>cd \Dev\workplace\imooc
#Start the Virtual Environment
D:\Dev\workplace\imooc>D:\Dev\env\imooc_venv\Scripts\activate #Exiting a Virtual Environment (imooc_venv) D:\Dev\workplace\imooc>D:\Dev\env\imooc_venv\Scripts\deactivate.bat D:\Dev\workplace\imooc> Copy the code
At this point, the iMOOc project Python virtual environment installation is complete, ღ(´ · ᴗ · ‘)
Domestic PIP source, accelerate class library installation
If PIP download speed is slow, you can consider switching to other source images in China
Domestic PIP sources:
- Tsinghua university: https://pypi.tuna.tsinghua.edu.cn/simple
- Ali cloud: https://mirrors.aliyun.com/pypi/simple
- China university of science and technology, https://pypi.mirrors.ustc.edu.cn/simple
- Douban: https://pypi.douban.com/simple
The temporary format is PIP install -i domestic source URL module name
For example: PIP install -i urllib3 at https://pypi.douban.com/simple
Use douban source to speed up the installation of URllib3 module