gogoWebsite

【conda】【pip】Guidelines for generating ``` files using `pip` and `conda`

Updated to 2 hours ago

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 belowpipandcondageneratefiles and solve common problems.

1. Usepipgenerate

1.1 Batch export of all components

Usually, we usepip freezeCommand 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 freezeCaptured 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 listOrder:

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 installationComponents in files

To get fromBulk installation packages in the file, can be usedcondaofinstallOrder:

pip install -r 

2. UsecondaExport and install

2.1 Batch export of all components

condaYou can export a list of all packages in your environment, but the default one isconda listThe command will not be generatedFile in format. To export a package list, you can use the following command:

conda list --export > 

2.2 Bulk installationComponents in files

To get fromBulk installation packages in the file, can be usedcondaofinstallOrder:

conda install --yes --file 

3. Frequently Asked Questions and Solutions

3.1 pip freezeOutput file path instead of version number

As mentioned earlier, this is usually becausepipThe package built from the local source is installed. The solution is to usepip list --format=freezeTo get the version number.

3.2 GeneratedFile does not include all dependencies

pip freezeOnly 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.venvorvirtualenv) Create a clean environment, then reinstall all dependencies in that environment and generate

3.3 conda list --exportFile format is incompatible

conda list --exportThe generated file contains additional information, which may require manual cleaning. To ensure thatpipCompatible with format, can be usedconda env exportgenerateFiles, then manually convert toFile, or use it directlyFile 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.