npm python pip是否有node的package.json的等价物?

kninwzqo  于 11个月前  发布在  Python
关注(0)|答案(5)|浏览(112)

在NodeJS的npm中,你可以创建一个package.json文件来跟踪你的项目依赖。当你想安装它们时,你只需运行npm install,它就会查看你的包文件,然后用一个命令安装它们。
当分发我的代码时,python是否有一个等效的概念,或者我是否需要在我的README中告诉人们安装每个依赖项:

pip install package1
pip install package2

字符串
才能用我的代码?

brc7rcf0

brc7rcf01#

一旦添加了所有必要的包

pip freeze > requirements.txt

字符串
创建需求文件。

pip install -r requirements.txt


在生产过程中再次安装这些软件包。

2wnc66cl

2wnc66cl2#

最好的方法可能是pipenv。我个人使用它。
然而,在本指南中,我将解释如何只使用Python和pip。而不使用pipenv。这是第一部分。它将给予我们一个关于pipenv如何工作的很好的理解。还有第二部分处理pipenv。检查部分pipenv(更接近npm)

Python和pip

为了让它与Python的一切都很好。这里的主要元素:

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

虚拟环境及其原因

注意,这个包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 frameworkdoc上的解释。
https://flask.palletsprojects.com/en/2.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


请参阅如何检查您在环境中的部分(使用哪个(Linux,unix)或在哪里(windows)。

要停用,请使用

> deactivate

需求文件

https://pip.pypa.io/en/latest/user_guide/#requirements-files
“需求文件”是包含依赖项列表的文件,使用pip install安装,如下所示
如何安装需求文件

pip install -r requirements.txt


需求文件用于保存pip freeze的结果,以实现可重复安装。在这种情况下,您的需求文件包含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(自己修复bug,而不是等待)

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(虚拟环境)为每个项目。项目是孤立的。然后冻结命令将只列出安装在该特定主机上的软件包。这使它的项目基地。冻结命令使软件包的列表在它的运行时间。与确切的版本匹配。我们从它生成一个需求文件(requirements.txt)。我们可以将其添加到项目存储库中。并安装依赖项。
整个过程可以在这个意义上完成:
Linux/unix

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

Windows

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

克隆存储库后的第一次设置。
创建新的env。
然后激活它。
然后安装所需的软件包。
否则,这里有一个完整的指南,关于使用requiremnets文件和虚拟环境安装软件包,来自官方文档:https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
第二个指南也很好地展示了这一点:https://docs.python.org/3/tutorial/venv.html
链接列表(已列出):

  • https://pip.pypa.io/en/latest/user_guide/#requirements-files
  • https://pip.pypa.io/en/latest/cli/pip_install/#requirements-file-format
  • https://pip.pypa.io/en/latest/cli/pip_freeze/#pip-freeze

pipenv(越接近npm)🔥

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

pipenv是一个类似于python的npm的工具。是pip的一个超级集合。

pipenv为我们创建虚拟环境。并管理依赖关系。

一个很好的特性是能够像文件一样编写packages.json。其中也有脚本部分。
Executing pipfile scripts
run python command with alias in command line like npm

安装

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

virtualenv-mapping-caveat

https://pipenv.pypa.io/en/latest/installation/#virtualenv-mapping-caveat
对我来说,在项目中创建env(就像node_modules一样)应该是默认的。确保激活它。通过设置环境变量。
pipenv似乎更方便。
主要是管理运行脚本太好了,不能错过。一个工具,简化了这一切。

基本用法及与npm的对比

请注意,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"

有类似package.lock的Pipfile.lock
运行npm install equivalent,运行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 env变量。要使pipenv在项目内设置虚拟环境。该虚拟环境在名为**.venv**的目录中创建(相当于node_modules)。

同时运行pipenv install**,目录中没有PipFile。也不是虚拟环境。将在.venv目录(node_modules equiv)上创建虚拟环境。并生成PipFilePipfile.lock.**

x1c 0d1x的数据
安装 flask 示例:

pipenv install flask

作为dev依赖项安装

pipenv install watchdog -d

pipenv install watchdog -dev

就像NPM一样。
pipenv所有命令(pipenv -h)

Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  --where                         Output project home information.
  --venv                          Output virtualenv information.
  --py                            Output Python interpreter information.
  --envs                          Output Environment Variable options.
  --rm                            Remove the virtualenv.
  --bare                          Minimal output.
  --man                           Display manpage.
  --support                       Output diagnostic information for use in
                                  GitHub issues.
  --site-packages / --no-site-packages
                                  Enable site-packages for the virtualenv.
                                  [env var: PIPENV_SITE_PACKAGES]
  --python TEXT                   Specify which version of Python virtualenv
                                  should use.
  --clear                         Clears caches (pipenv, pip).  [env var:
                                  PIPENV_CLEAR]
  -q, --quiet                     Quiet mode.
  -v, --verbose                   Verbose mode.
  --pypi-mirror TEXT              Specify a PyPI mirror.
  --version                       Show the version and exit.
  -h, --help                      Show this message and exit.

Usage Examples:
   Create a new project using Python 3.7, specifically:
   $ pipenv --python 3.7

   Remove project virtualenv (inferred from current directory):
   $ pipenv --rm

   Install all dependencies for a project (including dev):
   $ pipenv install --dev

   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

   Show a graph of your installed dependencies:
   $ pipenv graph

   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   Use a lower-level pip command:
   $ pipenv run pip freeze

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.
  requirements  Generate a requirements.txt from Pipfile.lock.
  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.
  upgrade       Resolves provided packages and adds them to Pipfile, or (if no
                packages are given), merges results to Pipfile.lock
  verify        Verify the hash in Pipfile.lock is up-to-date.

命令帮助

pipenv install -h

requirements.txt导入
https://pipenv.pypa.io/en/latest/pipfile/#importing-from-requirements-txt

pipenv环境管理

  • https://pipenv.pypa.io/en/latest/shell/
  • https://pipenv.pypa.io/en/latest/credentials/#injecting-credentials-into-pipfile-via-environment-variables
  • https://pipenv.pypa.io/en/latest/configuration/#configuration-with-environment-variables
    pipenv运行

要在项目虚拟环境中运行任何东西,您需要使用pipenv run
就像pipenv run python server.py一样。

自定义scripts快捷方式

npm中的scripts。或tasks
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部署

  • https://pipenv.pypa.io/en/latest/advanced/#using-pipenv-for-deployments

pipenv cli渲染也做得很好:



请务必阅读基本指南。和主要的doc link,确实包含所有的链接。
你可以看到pipenv有多丰富。

mspsb9vt

mspsb9vt3#

安装完所有软件包后,运行

pip freeze > requirements.txt

字符串
这会将软件包详细信息保存在文件requirements.txt中。
要进行安装,请运行

pip install -r requirements.txt


安装requirements.txt指定的应用程序。

at0kjp5o

at0kjp5o4#

是的,它被称为需求文件:
https://pip.pypa.io/en/stable/cli/pip_install/#requirement-specifiers
您可以指定软件包名称和版本号。
你也可以指定一个git url或者本地路径。
在通常情况下,您可以指定软件包,然后是版本号,例如。

sqlalchemy=1.0.1

字符串
可以通过以下命令安装requirements.txt文件中指定的所有软件包

pip install -r requirements.txt

6pp0gazn

6pp0gazn5#

我想在这里建议使用 pipenv。使用 Pipenv 管理软件包更容易,因为它可以为您管理软件包的列表和版本,因为我认为您需要在每次更改软件包时运行pip freeze命令。
它需要一个 Pipfile。这个文件将包含所有你需要的包及其版本,就像package.json一样。
您可以使用pipenv install/uninstall/update <package>删除/更新/添加项目
这也会为你的项目生成一个依赖关系树。
查看 Pipfiles 上的文章
Learn more about Pipenv

相关问题