[英]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.在 Node.js 中,我们为一个项目创建了一个 package.json 文件,其中列出了所有依赖项,以便 NPM 可以自动安装它们。
Is there an equivalent way to do this with Python ?有没有一种等效的方法可以用 Python 做到这一点?
Node has npm
similarly python having pip Node 有
npm
类似 python 有pip
pip is a package management system used to install and manage software packages written in Python.
pip 是一个包管理系统,用于安装和管理用 Python 编写的软件包。
So first you need to install pip,所以首先你需要安装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
.您必须将您的要求保留在项目文件夹中的
requirements.txt
中,就像nodejs
package.json
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.在交付具有依赖项的程序时,我发现的最快和最好的方法是使用几个简单的步骤。
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.pip install pipreqs
允许运行pipreqs /--directory to your program folder--/
并在您的程序文件夹中为您的程序生成 requirements.txt 文件,该文件列出了所有包依赖项。pip uninstall -r requirements.txt
and run the program to make sure it still works without the dependencies installed.pip uninstall -r requirements.txt
并运行该程序以确保它在没有安装依赖项的情况下仍然有效。你可以使用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.如果您要创建独立库,请考虑使用
setuptools
并为库所需的依赖项定义带有install_requires
字段的setup.py
。 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.如果您正在尝试生成开发/构建环境,则
requirements.txt
就足够了,但是通过标准的依赖项解析,其他 Python 包无法使用它。
See install_requires
vs. Requirement files查看
install_requires
与需求文件
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
).另外,请参考Python 打包指南以获取有关如何使用包的更全面的信息集,例如包的安装(包括 pip 的使用,virtualenv 对设置开发环境很有用), 生成的包可以分发(包括在项目的
setup.py
放入的内容)。
pipenv
is now the officially recommended packaging tool for installing packages and managing dependencies. pipenv
现在是官方推荐的用于安装包和管理依赖项的打包工具。 It bundles the great features found in related tools like virtualenv, pipfile, and more.它捆绑了相关工具(如 virtualenv、pipfile 等)中的强大功能。 It also gives you the best features from other worlds like
npm
, yarn
, etc.它还为您提供了来自其他领域的最佳功能,例如
npm
、 yarn
等。
Though, pip
is available with Python by default.不过,默认情况下
pip
可用于 Python。 It is the most common tool used for installing python packages.它是用于安装 python 包的最常用工具。 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.当作者发布 Python 包时,他们会包含一个
setup.py
文件,该文件将pip
定向到依赖项,以便它可以在您的环境中解析它们。 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
作者还可以包含一个或多个
requirements.txt
文件,您可以通过这些文件直接pip
收集所需的包 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.通常来说,你可能使用的几乎所有包都发布在 PyPI 存储库中,你只需要执行
pip install <package name>
并计算出其余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.