python pip是否相当于node的package.json?

时间:2018-02-23 04:27:24

标签: python node.js npm pip

在NodeJS的npm中,您可以创建一个package.json文件来跟踪项目依赖项。如果要安装它们,只需运行npm install,它就会查看您的包文件,并使用该单个命令安装它们。

在分发我的代码时,python是否有相同的概念,或者我需要告诉我的README中的人安装每个依赖项,如下所示:

pip install package1
pip install package2

在他们使用我的代码之前?

5 个答案:

答案 0 :(得分:3)

是的,它称为需求文件:

https://pip.readthedocs.io/en/1.1/requirements.html

您可以指定包名称&版本号。

您还可以指定git网址或本地路径

在通常情况下,您应指定包后跟版本号的数据,例如

sqlalchemy=1.0.1

您可以通过命令

安装requirements.txt文件中指定的所有软件包
pip install -r requirements.txt

答案 1 :(得分:2)

添加所有必要的包

pip freeze > requirements.txt

创建需求文件。

pip install -r requirements.txt

再次安装这些包,比如说在生产过程中。

答案 2 :(得分:2)

我想在这里推荐 pipenv。使用 Pipenv 管理包更容易,因为它会为您管理包的列表和版本,因为我认为每次对包进行更改时都需要运行 pip freeze 命令。

它需要一个 Pipfile。该文件将包含您需要的所有包及其版本,就像 package.json 一样。

您可以使用 pipenv install/uninstall/update <package>

删除/更新/添加项目

这也会为您的项目生成一个依赖树。就像 package-lock.json

Checkout this post on Pipfiles

Learn more about Pipenv

答案 3 :(得分:2)

最好的方法可能是pipenv!我个人使用它!

但是在本指南中,我将解释如何仅使用 python 和 pip 来实现!而且没有pipenv!这是第一部分!它将让我们很好地了解 pipenv 的工作原理!还有第二部分处理pipenv!检查部分pipenv(更接近 npm)

Python 和 pip

用python搞定一切!这里的主要元素:

  • 虚拟环境
  • 需求文件(包列表)
  • pip 冻结命令
  • 如何从需求文件安装包

虚拟环境及原因

请注意,为此要使用包 venv!这是官方的!并附带从 3.3+ 开始的 python 3 安装!

要了解它是什么以及为什么要查看它 https://docs.python.org/3/tutorial/venv.html

简而言之!一个虚拟环境将帮助我们管理一个独立版本的 python 解释器!安装的软件包也是如此!这样!不同的项目不必依赖相同的包安装而不得不冲突!阅读上面解释的链接并展示它!

<块引用>

... 这意味着一个 Python 安装可能无法满足每个应用程序的要求。如果应用程序 A 需要特定模块的 1.0 版,而应用程序 B 需要 2.0 版,则需求存在冲突,安装 1.0 或 2.0 版将导致一个应用程序无法运行。

您可能想查看 flask 框架 文档中的说明!

https://flask.palletsprojects.com/en/1.1.x/installation/#virtual-environments

为什么我们关心这个并且应该使用它!隔离项目! (每个都有它的环境)!然后冻结命令将在每个项目基础上工作!检查最后一节

用法

这里有一个关于如何设置和工作的很好的指南:

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

先检查安装部分!

然后

创建虚拟环境,请转到项目目录并运行:

在 macOS 和 Linux 上:

> python3 -m venv env

在 Windows 上:

> py -m venv env
<块引用>

注意您应该使用 .gitignore 或类似文件从版本控制系统中排除您的虚拟环境目录。

要开始使用控制台中的环境,您必须激活它

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#activating-a-virtual-environment

在 macOS 和 Linux 上:

> source env/bin/activate

在 Windows 上:

> .\env\Scripts\activate

请参阅有关如何检查您在环境中的部分(使用 which (linux, unix) 或 where (windows)!)

要停用您使用

> deactivate

需求文件

https://pip.pypa.io/en/latest/user_guide/#requirements-files

<块引用>

“需求文件”是包含要使用 pip install 安装的依赖项列表的文件

(如何安装需求文件)

pip install -r requirements.txt
<块引用>

需求文件用于保存 pip 冻结的结果,以实现可重复安装。在这种情况下,您的需求文件包含 pip freeze 运行时安装的所有内容的固定版本

python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt

一些语法:

pkg1
pkg2==2.1.0
pkg3>=1.0,<=2.0

== 精确!

requests==2.18.4
google-auth==1.1.0

强制 pip 接受早期版本

ProjectA
ProjectB<1.3

使用带有标签的 git(自己修复错误而不是等待)

git+https://myvcs.com/some_dependency@sometag#egg=SomeDependency

再次检查链接 https://pip.pypa.io/en/latest/user_guide/#requirements-files 我从他们那里挑选了所有的例子!你应该看看解释!和细节!

有关格式详细信息,请检查:https://pip.pypa.io/en/latest/cli/pip_install/#requirements-file-format

冻结命令

Pip 可以使用 freeze 命令导出所有已安装软件包及其版本的列表!在运行命令时!列出当前环境中所有已安装包的列表!

pip freeze

将输出如下内容:

cachetools==2.0.1
certifi==2017.7.27.1
chardet==3.0.4
google-auth==1.1.1
idna==2.6
pyasn1==0.3.6
pyasn1-modules==0.1.4
requests==2.18.4
rsa==3.4.2
six==1.11.0
urllib3==1.22

我们可以将其写入需求文件中

pip freeze > requirements.txt

https://pip.pypa.io/en/latest/cli/pip_freeze/#pip-freeze

安装包恢复

通过为每个项目使用 venv(虚拟环境)!这些项目是孤立的!然后 freeze 命令将仅列出安装在该特定环境中的软件包!这是由项目基地组成的!冻结命令在运行时列出软件包!与确切版本匹配!我们从中生成一个需求文件(requirements.txt)!我们可以将其添加到项目仓库中!并安装依赖项!

从这个意义上说,整体可以做到:

Linux/Unix

python3 -m venv env
source env/bin/activate
pip3 install -r requirements.txt

视窗

py -m venv env
.\env\Scripts\activate
pip3 install -r requirements.txt

克隆 repo 后的第一次设置!

创建新环境!

然后激活它!

然后安装需要的包!

否则这里有一个关于使用官方文档中的 requiremnets 文件和虚拟环境安装软件包的完整指南:https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

第二个指南也显示得很好:https://docs.python.org/3/tutorial/venv.html

链接列表(已列出):

pipenv (更接近 npm)

https://pipenv.pypa.io/en/latest/

pipenv 是一个尝试像 npm for python 一样的工具!是超集pip!

pipenv 为我们创建虚拟环境!并管理依赖项!

一个很好的功能是能够像文件一样写入packages.json!其中也包含脚本部分!

Executing pipfile scripts

run python command with alias in command line like npm

安装

https://pipenv.pypa.io/en/latest/install/

virtualenv-mapping-警告

https://pipenv.pypa.io/en/latest/install/#virtualenv-mapping-caveat

对我来说,在项目中创建 env(就像 node_modules 一样)甚至应该是默认设置!一定要激活!通过设置环境变量!

pipenv 看起来更方便!

主要管理运行脚本太好了,不容错过!一个可以简化一切的工具!

基本用法与 npm 对比

https://pipenv.pypa.io/en/latest/basics/

(请务必查看上面的指南以熟悉基础知识)

注意 npm package.json 的等价物是 PipFile 文件!

示例:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
flask = "*"
simplejson = "*"
python-dotenv = "*"

[dev-packages]
watchdog = "*"

[scripts]
start = "python -m flask run"

[requires]
python_version = "3.9"

Pipfile.lock 就像 package.lock

运行 npm install 等效!你跑pipenv install

安装新包

pipenv install <package>
<块引用>

如果不存在,这将创建一个 Pipfile。如果确实存在,它将使用您提供的新包自动进行编辑。

就像使用 npm!

$ pipenv install "requests>=1.4"   # will install a version equal or larger than 1.4.0
$ pipenv install "requests<=2.13"  # will install a version equal or lower than 2.13.0
$ pipenv install "requests>2.19"   # will install 2.19.1 but not 2.19.0

如果设置了 PIPENV_VENV_IN_PROJECT=1 环境变量!使pipenv在项目中设置虚拟环境!它是在名为 .venv(相当于 node_modules)的目录中创建的。

也在目录中没有 PipFile 的情况下运行 pipenv install!既不是虚拟环境!将在 .venv 目录(node_modules equiv)上创建虚拟环境!并生成一个 PipFilePipfile.lock

enter image description here

安装烧瓶示例:

pipenv install flask

安装为开发依赖

pipenv install watchdog -d

pipenv install watchdog -dev

就像使用 npm!

pipenv 所有命令 (pipenv -h)

Commands:
  check      Checks for PyUp Safety security vulnerabilities and against PEP
             508 markers provided in Pipfile.

  clean      Uninstalls all packages not specified in Pipfile.lock.
  graph      Displays currently-installed dependency graph information.
  install    Installs provided packages and adds them to Pipfile, or (if no
             packages are given), installs all packages from Pipfile.

  lock       Generates Pipfile.lock.
  open       View a given module in your editor.
  run        Spawns a command installed into the virtualenv.
  scripts    Lists scripts in current environment config.
  shell      Spawns a shell within the virtualenv.
  sync       Installs all packages specified in Pipfile.lock.
  uninstall  Uninstalls a provided package and removes it from Pipfile.
  update     Runs lock, then sync.

命令帮助

pipenv install -h

requirements.txt

导入

https://pipenv.pypa.io/en/latest/basics/#importing-from-requirements-txt

使用 pipenv 进行环境管理

https://pipenv.pypa.io/en/latest/basics/#environment-management-with-pipenv

pipenv 运行

要在项目虚拟环境中运行任何东西,您需要使用 pipenv run

就像pipenv run python server.py

自定义脚本快捷方式

npm 中的脚本!

https://pipenv.pypa.io/en/latest/advanced/#custom-script-shortcuts

[scripts]
start = "python -m flask run"

然后运行

pipenv run start

就像使用 npm!

<块引用>

如果你想要锁文件的 requirements.txt 输出,运行 $ pipenv lock -r。然而,这将包括所有散列(这很棒!)。要获得不带哈希值的 requirements.txt,请使用 $ pipenv run pip freeze。

还要提一下pipenv cli渲染做得很好:

enter image description here

请务必阅读基础指南!

你可以看到pipenv有多丰富!

答案 4 :(得分:1)

安装完所有软件包后,请执行以下操作:

pip freeze > requirements.txt

这将在文件requirements.txt中添加包详细信息 现在, 对于安装:

pip install -r requirements.txt` 

它将完成安装包的工作

相关问题