Foreword:
在window中使用pipenv创建虚拟环境时,虚拟环境默认的位置是在C:\User\Administrator\.virtualenvs\目录下;
那如果我们想配置到自定义位置,该如何修改呢?
When we are developing a python project, in order to prevent conflicts between the python environments between projects, it is necessary to configure a virtual environment so that the packages between projects are isolated from each other and do not affect each other.
1. Method 1: virtualenv
1. Install the virtual environment library virtualenv
Open the cmd command line and enter the command pip/pip3 install virtualenv, as shown below:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv
2. Create a virtual environment
Use the command line to switch to the directory where , and use virtualenv test1 (test1 is the file name created by yourself, you can choose it at will), as shown in the figure below:
Open the corresponding folder to see the corresponding
However, after the virtual environment is created at this time, the command line will not automatically enter the virtual environment, so it is recommended to use method 2 to create a virtual environment.
2. Method 2: virtualenvwrapper-win (recommended)
Method 1 After the virtual environment is created, it does not automatically enter the virtual environment, and requires some operations to enter the virtual environment. But we hope to automatically enter the virtual environment after creating the virtual environment, so it is recommended to use method 2 to create a virtual environment.
(1) Download the virtual environment library virtualenvwrapper-win
Execute the following code to install the virtualenvwrapper-win library
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenvwrapper-win
Test: Create a virtual environment with virtualenvwrapper-win.
(2) Input workon
, check whether the installation is successful.
Note: This prompt is actually installed successfully, it just prompts that no virtual environment has been created in this directory.
(3) Create a virtual environment directory file (★★★)
Initially, the created virtual environment will be on the C drive by default. We can create a file dedicated to storing the virtual environment on other large-capacity hard disks, which only needs to create a new system variable WORKON_HOME in the system environment variable.
In the future, the virtual environment will be placed in F:\PythonProject\virtualenvsFile for management.
Use echo %WORKON_HOME%
to check whether the configuration is successful, and if the path is correct, create a virtual environment
If the above is not successful, re-exit the cmd window and try again by opening a cmd window. .
Create a virtual environment
(4) Common commands
mkvirtualenv 虚拟环境名称
workon # 可查看目前有哪些虚拟环境
workon 名字 # 可进入具体的虚拟环境下工作
deactivate #退出虚拟环境
rmvirtualenv [name] 删除虚拟环境
pip install -r requirements.txt 导入项目时,根据项目中的requirements.txt文件,安装相应库
Note: There are other ways to create a virtual environment. This chapter does not introduce them one by one. Those who are interested can refer to them by themselves.