My directory title
- 1. Create a new Python virtual environment
- 2. View the path of the virtual environment
- 3. Export all libraries and version numbers of the virtual environment to a requirements.txt file
- 4. If you only need to export all libraries and version numbers installed in the current virtual environment to a requirements.txt file without including the source of each library, you can use the following command:
- 5. Install all packages in requirements.txt in the new virtual environment
1. Create a new Python virtual environment
The steps to create a new Python virtual environment using conda in the command window are as follows:
- Confirm conda is installed. If it is not installed, install conda first.
- Open a command line or terminal window.
- Enter the following command to create a new Python virtual environment:
conda create --name [环境名称] python=[Python版本号]
For example, to create a Python 3.9 environment named myenv, enter the following command:
conda create --name myenv python=3.9
- To install the required packages, you can use pip or conda commands, such as:
conda activate myenv
conda install numpy scipy pandas
Pay attention to using conda activate
the command to activate the virtual environment before installing the package.
After creating the virtual environment, you can activate the virtual environment through conda activate myenv
(Windows/Linux) or (Mac). After entering the virtual environment, you can install the required packages and run Python scripts in it.source activate myenv
2. View the path of the virtual environment
You can view the path of this virtual environment using the following command:
- On Windows, activate the virtual environment using the activate command, and then execute the following command at the command line:
echo %CONDA_PREFIX%
If you are using PowerShell, use the following command:
$ENV:CONDA_PREFIX
This will output the path to the virtual environment.
- On Linux and macOS, activate the virtual environment using the activate command, and then execute the following command at the command line:
echo $CONDA_PREFIX
This will output the path to the virtual environment.
If you have installed vscode, you can also use vscode to view the path of the virtual environment. Open vscode, select "View" -> "Command Panel" -> "Python: Select Interpreter", and then you can see all Python interpreters installed in the current system, including virtual environments. Hover your mouse over a virtual environment to see the path to that virtual environment.
3. Export all libraries and version numbers of the virtual environment to a requirements.txt file
You can export all libraries and version numbers for this virtual environment into a requirements.txt file using the following command:
conda activate <虚拟环境名称>
conda list --explicit > requirements.txt
This will activate the virtual environment and conda list
list all installed libraries in that virtual environment with their version information using the command and >
redirecting them to the requirements.txt file.
If you do not use --explicit
the parameter, you can only view the name and version information of each library, and cannot determine the source of each library. Using --explicit
the parameter you can view the source of each library, which is useful when you need to reproduce the virtual environment on another machine.
The content format of the requirements.txt file is:
/path/to/package/pkg-0.1.0-1.tar.bz2
/path/to/package/pkg2-1.0.0-py37_0.tar.bz2
/path/to/package/pkg3-0.2.0-1.tar.bz2
4. If you only need to export all libraries and version numbers installed in the current virtual environment to a requirements.txt file without including the source of each library, you can use the following command:
conda activate <虚拟环境名称>
conda env export --no-builds | findstr /v /c:"prefix" > requirements.txt
This will activate the virtual environment and conda env export
export all library and version information using the command with --no-builds
the parameter indicating that the build specification is not included. findstr /v /c:"prefix""
The command will remove one of the lines containing "prefix", which contains the virtual environment path information. If this line is not removed, problems may occur during installation. Finally, redirect the exported results to the requirements.txt file.
The content format of the requirements.txt file is:
name: <虚拟环境名称>
channels:
- defaults
dependencies:
- python=3.9
- pip=21.1.2
- numpy=1.21.0
- pandas=1.3.0
- matplotlib=3.4.2
- seaborn=0.11.1
where name
the line indicates the name of the virtual environment, channels
the line indicates the conda channel to use (by default it is "defaults"), dependencies
and each line in represents an installed library and its version number.
You can use conda create
the command to create a new virtual environment based on the requirements.txt file. For example, to install all the above packages in a new virtual environment, you can execute the following command:
conda create --name <新虚拟环境名称> --file requirements.txt
This will create a <新虚拟环境名称>
new virtual environment named and install all libraries and their version information from the requirements.txt file.
5. Install all packages in requirements.txt in the new virtual environment
You can use conda create
the command to create a new virtual environment based on the requirements.txt file. For example, to install all the above packages in a new virtual environment, you can execute the following command:
conda create --name <新虚拟环境名称> --file requirements.txt
This will create a <新虚拟环境名称>
new virtual environment named and install all libraries and their version information from the requirements.txt file.