SoFunction
Updated on 2025-04-05

How to generate Python

In a Python project,Files are usually used to list the libraries and their version numbers that the project depends on. This allows easy installation of the same dependencies in other environments.

The following is generatedSeveral ways to file:

Method 1: Use pip freeze

If you have installed all required dependencies in a virtual environment or global environment, you can directly generate them using the following command

pip freeze > 

Features:

  • Lists all installed packages and their exact version numbers in the current environment.
  • It may contain some packages that your project does not require (such as development tools or dependencies for other projects).

Method 2: Export only the dependencies that are actually used by the project

To ensureIt only contains the dependencies that the project actually needs. You can manually record the dependency package, or use tools to analyze the import situation in the code.

Manually specify dependencies

Create aFile and manually add the dependencies you need. For example:

flask==2.3.2
numpy>=1.21.0
pandas
requests

Use the pipreqs tool

pipreqsis a tool that scans your project directory and automatically generates a list of dependencies required by the project.

1. Installationpipreqs

pip install pipreqs

2. Run the following command in the project root directory:

pipreqs ./ --encoding=utf8 --force
  • ./Represents the current directory.
  • --encoding=utf8Ensure UTF-8 encoding is supported.
  • --forceForce overwrite existing onesdocument.

3. GeneratedThe file will look like:

flask==2.3.2
numpy==1.21.0
pandas==1.3.5
requests==2.26.0

Method 3: Use poetry or pipenv to manage dependencies

If you are using modern dependency management tools (e.g.poetryorpipenv), they will automatically generate similarfile.

Using Poetry

Initialize the project:

poetry init

Add dependencies:

poetry add flask numpy pandas requests

Export as

poetry export -f  --output 

Using Pipenv

Initialize the project:

pipenv install flask numpy pandas requests

Export as

pipenv lock -r > 

Method 4: Generate from existing or

If your project is usedorFile definition dependencies can also be generated from them

from

ifDefined ininstall_requires, you can extract dependencies using the following command:

from setuptools import setup

setup(
    name="your_project",
    install_requires=[
        "flask==2.3.2",
        "numpy>=1.21.0",
        "pandas",
        "requests",
    ],
)

Then run:

pip install -e .
pip freeze > 

from

If you use(such as Poetry or Flit), you can export using the following command:

poetry export -f  --output 

Things to note

1. Version control

  • If you need to strictly lock the version, use==Specify the specific version number.
  • If version range is allowed, you can use>=or~=

2. Distinguish development dependencies

Development dependencies (such as test tools, formatting tools) can be listed separately in a file, for example

Example:

# 
flask==2.3.2
numpy>=1.21.0

# 
pytest==7.0.0
black

3. Virtual environment

It is recommended to generate in a virtual environment, to avoid including irrelevant dependencies in the global environment.

Through the above methods, you can easily generate suitable projectsdocument!

This is all about this article about how Python generates. For more related Python generation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!