在运行实际/主脚本之前,是否可以先包含/调用python模块(依赖项)安装?
例如,在我的main.py中:
import os, sys
import MultipartPostHandler
def main():
# do stuff here
但是尚未安装MultipartPostHandler,所以我想先安装它实际运行main.py ...,但以自动化方式进行。当我自动说时,我的意思是我将只调用脚本一次来启动依赖项安装,然后再执行主脚本的实际功能。(以某种方式,与maven有点类似。但是我只需要安装部分)
我已经知道setuptools的基础知识。问题是我可能必须分别调用安装(setup.py)和主脚本(main.py)。
非常感谢任何想法。预先感谢!
在运行实际/主脚本之前,是否可以先包含/调用python模块(依赖项)安装?
setuptools
并在install_requires
中明确列出它们。install_requires
功能,因此您可能还想提供main
。 我已经知道setuptools的基础知识。问题是我可能不得不分别调用安装(setup.py)和主脚本(main.py)。
通常不是问题。首先安装带有entry_points
文件和entry_points
的所有内容非常普遍。另外,如果您列出了依赖项,那么您可以有合理的期望,那就是在调用函数而不依赖于requirements.txt
时它将存在。这是一种合理的方法,可以期望存在所需的依赖项,并且仅将pip install -r requirements.txt
用于可选的依赖项。
创建这样的树结构:
try/except ImporError
您的代码将在try/except
下;让我们想象一些执行简单任务的代码:
$ tree
.
├── mymodule
│ ├── __init__.py
│ └── script.py
└── setup.py
这是一个相关的设置:
mymodule
[这将使您的# module/script.py
def main():
try:
import requests
print 'requests is present. kudos!'
except ImportError:
raise RuntimeError('how the heck did you install this?')
作为命令可用,并且还将照顾安装所需的依赖项(例如# setup.py
from setuptools import setup
setup(
name='mymodule',
packages=['mymodule'],
entry_points={
'console_scripts' : [
'mycommand = mymodule.script:main',
]
},
install_requires=[
'requests',
]
)
)
main
如果要使用requests
,则...
~tmp damien$ virtualenv test && source test/bin/activate && pip install mymodule/
New python executable in test/bin/python
Installing setuptools, pip...done.
Unpacking ./mymodule
Running setup.py (path:/var/folders/cs/nw44s66532x_rdln_cjbkmpm000lk_/T/pip-9uKQFC-build/setup.py) egg_info for package from file:///tmp/mymodule
Downloading/unpacking requests (from mymodule==0.0.0)
Using download cache from /Users/damien/.pip_download_cache/https%3A%2F%2Fpypi.python.org%2Fpackages%2F2.7%2Fr%2Frequests%2Frequests-2.4.1-py2.py3-none-any.whl
Installing collected packages: requests, mymodule
Running setup.py install for mymodule
Installing mycommand script to /tmp/test/bin
Successfully installed requests mymodule
Cleaning up...
(test)~tmp damien$ mycommand
requests is present. kudos!
有几种方法可以做到这一点。一种方法是用argparse
... # module/script.py
import argparse
def foobar(args):
# ...
def main():
parser = argparse.ArgumentParser()
# parser.add_argument(...)
args = parser.parse_args()
foobar(args)
块包围import
语句,然后在引发ImportError异常的情况下,提供一些Python代码来安装软件包,例如:
try
我认为这种方法不是很干净。另外,如果还有其他不相关的导入问题,此处将不会检测到。更好的方法可能是使用except ImportError
脚本来检查模块是否已安装:
try:
import MultipartPostHandler
except ImportError:
# code that installs MultipartPostHandler and then imports it
如果没有,则安装模块:
bash
然后我们可以安全地运行原始的Python代码。
编辑:实际上,我更喜欢FLORET的答案。 pip freeze | grep MultipartPostHandler
模块正是您想要的。
您应该使用pip install MultipartPostHandler
模块。这是一个例子:
imp
对于完整方法,请使用队列:
imp
您可以使用import imp
import httplib2
import sys
try:
import MultipartPostHandler
except ImportError:
# Here you download
http = httplib2.Http()
response, content = http.request('http://where_your_file_is.com/here')
if response.status == 200:
# Don't forget the right managment
with open('MultipartPostHandler.py', 'w') as f:
f.write(content)
file, pathname, description = imp.find_module('MultipartPostHandler')
MultipartPostHandler = imp.load_module('MultipartPostHandler', file, pathname, description)
else:
sys.exit('Unable to download the file')
下载二进制文件>
希望对您有帮助
BR