Table of contents
- 1. Use `pip` to generate ``
- 1.1 Batch export of `` containing all components
- 1.2 Bulk installation of components in the `` file
- 2. Export and install using `conda`
- 2.1 Batch export of `` containing all components
- 2.2 Bulk installation of components in the `` file
- 3. Frequently Asked Questions and Solutions
- 3.1 `pip freeze` output file path instead of version number
- 3.2 The generated `` file does not include all dependencies
- 3.3 `conda list --export` file format is incompatible
- 4. Benefits of using files
- 4.1 Manage dependencies
- 4.2 Sharing projects with others
In a Python project,Files are used to record and manage project dependencies. How to use it is described below
pip
andconda
generatefiles and solve common problems.
1. Usepip
generate
1.1 Batch export of all components
Usually, we usepip freeze
Command to generatedocument:
pip freeze >
However, sometimes something like the following appears in the generated file:
click @ file:///tmp/build/80754af9/click_1621604852318/work
Flask @ file:///home/ktietz/src/ci/flask_1611932660458/work
This situation showspip freeze
Captured the local path of the package, not the version number. This can cause problems when installing in different environments. The solution to this problem is to usepip list
Order:
pip list --format=freeze >
This ensuresThe file contains only the name and version number of the package, not the path. The output will look like:
appdirs==1.4.4
black==21.6b0
certifi==2021.5.30
click==8.0.1
colorama==0.4.4
dnspython==2.1.0
email-validator==1.1.3
Flask==1.1.2
Flask-Login==0.5.0
flask-mongoengine==1.0.0
1.2 Bulk installation
Components in files
To get fromBulk installation packages in the file, can be used
conda
ofinstall
Order:
pip install -r
2. Useconda
Export and install
2.1 Batch export of all components
conda
You can export a list of all packages in your environment, but the default one isconda list
The command will not be generatedFile in format. To export a package list, you can use the following command:
conda list --export >
2.2 Bulk installation
Components in files
To get fromBulk installation packages in the file, can be used
conda
ofinstall
Order:
conda install --yes --file
3. Frequently Asked Questions and Solutions
3.1 pip freeze
Output file path instead of version number
As mentioned earlier, this is usually becausepip
The package built from the local source is installed. The solution is to usepip list --format=freeze
To get the version number.
3.2 Generated
File does not include all dependencies
pip freeze
Only packages that are installed directly will be listed. Some projects may depend on other dependencies, which may not be listed. To ensure that all dependencies are included, virtual environment tools can be used (e.g.venv
orvirtualenv
) Create a clean environment, then reinstall all dependencies in that environment and generate。
3.3 conda list --export
File format is incompatible
conda list --export
The generated file contains additional information, which may require manual cleaning. To ensure thatpip
Compatible with format, can be usedconda env export
generateFiles, then manually convert to
File, or use it directly
File to create an environment:
conda env export >
conda env create -f
4. Benefits of using files
4.1 Manage dependencies
By listing the dependencies of the project in the file, you can easily see which packages are needed and which versions they need.
4.2 Sharing projects with others
If you share a project with others, you can include the files so they can easily install the packages they need, saving them time and reducing frustration, and helping to ensure everyone uses the same version of the package
Through the above methods, you can more effectively manage the dependencies of Python projects and ensure consistency between different environments.