Python项目的简单依赖关系管理 [英] Simple dependency management for a Python project
问题描述
我来自Java背景,是Python的新手.
I am coming from Java background and completely new at Python.
现在,我有一个小项目,其中包含一些包含一些导入的Python文件.我知道我的计算机上没有安装导入的依赖项,因此我尝试找出所需的依赖项并运行pip
进行安装.
Now I have got a small project with a few Python files that contain a few imports. I know I does not have the imported dependencies installed on my computer, so I try to figure out the required dependencies and run pip
to install them.
我想换个方式.我希望将依赖项列在单个文件中,并在构建过程中自动安装它们.
I would like to do it differently. I would prefer to have the dependencies listed in a single file and install them automatically during the build process.
这有意义吗?如果可以,我有几个问题:
Does it make sense ? If it does I have a few questions:
- 如何列出
pip
安装所需的项目依赖项? - 如何运行
pip
从列表中安装依赖项?
- How to list the project dependencies required to install by
pip
? - How to run
pip
to install the dependencies from the list ?
推荐答案
管理python项目依赖项的一种常用方法是通过项目根目录中的一个名为"requirements.txt"的文件.一种简单的方法是:
A common way to manage dependencies for a python project is via a file in root of the project named "requirements.txt". An easy way to make this is:
- 为您的项目设置python virtualenv
- 通过pip手动安装所需的模块
- 执行
pip freeze > requirements.txt
以生成需求文件
- Setup a python virtualenv for your project
- Manually install the required modules via pip
- Execute
pip freeze > requirements.txt
to generate the requirements file
然后可以使用pip install -r requirements.txt
将所有依赖项安装在其他位置.
You can then install all the dependencies in other locations using pip install -r requirements.txt
.
如果要在其他人pip install
软件包时自动安装依赖项,则可以在setup.py
中使用install_requires()
.
If you want dependencies to be installed automatically when other people pip install
your package, you can use install_requires()
in your setup.py
.
这篇关于Python项目的简单依赖关系管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!