简体   繁体   中英

Is there a way to automatically install required packages in Python?

In Node.js, for a project we create a package.json file, which lists all the dependencies, so that NPM can automatically install them.

Is there an equivalent way to do this with Python ?

Node has npm similarly python having pip

pip is a package management system used to install and manage software packages written in Python.

So first you need to install pip,

sudo apt-get install python-pip

You have to keep your requirements in requirements.txt in you project folder as like package.json in nodejs .

eg:

pip install package1
pip install package2
pip install package3

Then move on to your project path, then do this:

pip install -r requirements.txt

您可以使用pip freeze > requirements.txt生成依赖项列表,并使用pip install -r requirements.txt安装所有依赖项。

The quickest and best way that I've found when delivering a program that has dependencies uses a few simple steps.

  1. Use pip install pipreqs that allows running pipreqs /--directory to your program folder--/ and produces the requirements.txt file inside your program's folder for your program that lists all package dependencies.
  2. Copy/paste the folders containing package dependencies from where ever your python packages are downloaded to (eg..\\Python\\Python38-32\\Lib\\site-packages) straight into the folder for your program.
  3. Run pip uninstall -r requirements.txt and run the program to make sure it still works without the dependencies installed.

你可以使用conda

conda install --file requirements.txt

If you are creating a standalone library, consider using setuptools and define a setup.py with an install_requires field for the dependencies required for the library. Example:

from setuptools import setup

setup(
    name='example.package',
    install_requires=[
        'setuptools',
        'requests>=3.0.0',
        # other requirements
    ],
    # ... and other attributes
)

If you are trying to produce a development/build environment, a requirements.txt can be sufficient, but this is not available to other Python packages through the standard dependency resolution.

See install_requires vs. Requirement files

Also, please reference the Python packaging guide for a more comprehensive set of information on how to work with packages, eg installation of package (covers the usage of pip, virtualenv which is useful for setting up a development environment), producing a package that can be distributed (covers what to put in a project's setup.py ).

pipenv is now the officially recommended packaging tool for installing packages and managing dependencies. It bundles the great features found in related tools like virtualenv, pipfile, and more. It also gives you the best features from other worlds like npm , yarn , etc.

Though, pip is available with Python by default. It is the most common tool used for installing python packages. When authors publish Python packages, they include a setup.py file that directs pip to the dependencies so it can resolve them with your environment. Authors may also include one or more requirements.txt files which you can direct pip to gather the required packages IE pip install -r /path/to/requirements.txt

Typically speaking, almost all packages you might use are published in the PyPI repository and you merely need to do pip install <package name> and it figures out the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM