[英]How to automatically install required packages from a python script as necessary?
Is there anything in python or linux what basically instructs the system to "install whatever is necessary".在 python 或 linux 中有什么东西基本上指示系统“安装任何必要的东西”。 Basically I find it annoying to install python packages for each new script/system/server that I work on.
基本上我觉得为我工作的每个新脚本/系统/服务器安装 python 包很烦人。 Each time I end up doing a sudo pip or an apt-get or dnf anyway.
无论如何,每次我最终都会执行 sudo pip 或 apt-get 或 dnf。 Why not automate that within the script itself.
为什么不在脚本本身内自动化。 Whereever a 'no package found' error crops up, pass the library name to the install statement.
无论何时出现“找不到包”错误,请将库名称传递给安装语句。 Is this there ?
这是在吗?
PS: I know docker exists, but am talking at a python/script level or a direct system level for purely execution purposes. PS:我知道 docker 存在,但我只是在 python/脚本级别或直接系统级别进行讨论,纯粹是为了执行目的。
Thanks谢谢
How to automatically install required packages from a python script as necessary?
如何根据需要从 python 脚本自动安装所需的包?
Let's assume that your Python script is example.py
:假设您的 Python 脚本是
example.py
:
import os
import time
import sys
import fnmatch
import requests
import urllib.request
from bs4 import BeautifulSoup
from multiprocessing.dummy import Pool as ThreadPool
print('test')
You can use pipreqs to automatically generate a requirements.txt
file based on the import
statements that the Python script(s) contain.您可以使用pipreqs根据 Python 脚本包含的
import
语句自动生成一个requirements.txt
文件。 To use pipreqs
, assuming that you are in the directory where example.py
is located:要使用
pipreqs
,假设您在example.py
所在的目录中:
pip install pipreqs
pipreqs .
It will generate the following requirements.txt
file:它将生成以下
requirements.txt
文件:
requests==2.23.0
beautifulsoup4==4.9.1
which you can install with:您可以安装:
pip install -r requirements.txt
The best way I know is, create a file requirements.txt
list out all the packages name in it and install it using pip我知道的最好方法是,创建一个文件
requirements.txt
列出其中的所有包名称并使用pip安装它
pip install -r requirements.txt
Example requirements.txt
:示例
requirements.txt
:
BeautifulSoup==3.2.0
Django==1.3
Fabric==1.2.0
Jinja2==2.5.5
PyYAML==3.09
Pygments==1.4
SQLAlchemy==0.7.1
South==0.7.3
amqplib==0.6.1
anyjson==0.3
...
You can use setuptools to install dependencies automatically when you install your custom project on a new machine.当您在新机器上安装自定义项目时,您可以使用 setuptools 自动安装依赖项。 Requirements file works just fine if all you want to do is to install a few
PyPI
packages.如果您只想安装一些
PyPI
包,需求文件就可以正常工作。
Here is a nice comparison between the two. 这是两者之间的一个很好的比较。 From the same link you can see that if your project has two dependent packages
A
and B
, all you have to include in your setp.py file is a line从同一个链接你可以看到,如果你的项目有两个依赖包
A
和B
,你必须在你的 setp.py 文件中包含一行
install_requires=[
'A',
'B'
]
Of course, setuptools
can do much more.当然,
setuptools
可以做的更多。 You can include setups for external libraries (say C files), non PyPI dependencies, etc. The documentation gives a detailed overview on installing dependencies.您可以包括外部库(例如 C 文件)、非 PyPI 依赖项等的设置。 文档提供了有关安装依赖项的详细概述。 There is also a really good tutorial on getting started with python packaging.
还有一个非常好的 Python 打包入门教程。
From their example, a typical setup.py file would look like this.从他们的例子来看,典型的 setup.py 文件看起来像这样。
from setuptools import setup
setup(name='funniest',
version='0.1',
description='The funniest joke in the world',
url='http://github.com/storborg/funniest',
author='Flying Circus',
author_email='flyingcircus@example.com',
license='MIT',
packages=['funniest'],
install_requires=[
'markdown',
],
zip_safe=False)
In conclusion, it is so simple to get started with setuptools
.总之,开始使用
setuptools
非常简单。 This package can make it fairly easy to migrate your code to a new machine.这个包可以很容易地将你的代码迁移到新机器上。
mv requirements.txt requirements.txt.orig
pipreqs --force ./
and和
cat requirements.txt
猫需求.txt
Keras==2.2.4
scipy==1.3.0
numba==0.44.1
imgaug==0.2.9
opencv_python==4.1.0.25
BUT, some modules needs binary compilation (where you don't have libraries) and some could be installed by system package manager with different names.但是,有些模块需要二进制编译(您没有库),有些可以由系统包管理器使用不同的名称安装。
I'm not really sure about auto installing what is necessary, but it you stop on using requirements.txt , there are 3 approaches:我不太确定自动安装什么是必要的,但如果你停止使用requirements.txt ,有 3 种方法:
pip freeze > requirements.txt
or pipreqs
for less messy result.pip freeze > requirements.txt
或pipreqs
执行,以减少混乱的结果。 There are many answers for the 1-st option on stackoverflow, the 2-d option is self-explanatory, so I would like to describe the 3-d approach. stackoverflow 上的 1-st 选项有很多答案,2-d 选项不言自明,所以我想描述 3-d 方法。 There is a library called to-requirements.txt .
有一个名为to-requirements.txt的库。 To install it type this:
要安装它,请键入:
pip install to-requirements.txt # Pip install to requirements.txt
If you read the whole command at once you would see, what it does.如果您一次阅读整个命令,您会看到它的作用。 After installing you should setup it.
安装后你应该设置它。 Run:
跑:
requirements-txt setup
It overrides the pip scripts so that each pip install
or pip uninstall
updates the requirements.txt file of your project automatically with required versions of packages.它会覆盖 pip 脚本,以便每个
pip install
或pip uninstall
使用所需版本的包自动更新项目的 requirements.txt 文件。 The overriding is made safely, so that after uninstalling this package the pip will behave ordinary.覆盖是安全的,因此在卸载此包后,pip 将表现正常。
And you could customize the way it works.您可以自定义它的工作方式。 For example, disable it globally and activate it only for the required directories, activate it only for git repositories, or allow / disallow to create requirements.txt file if it does not exist.
比如全局禁用,只对需要的目录激活,只对git仓库激活,或者如果不存在则允许/禁止创建requirements.txt文件。
Links:链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.