热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何下载python模块_如何在运行时安装和导入Python模块?

您可以导入pip而不是使用子进程:importpipdefinstall(package):pip.main([install,package])#Examp

您可以导入pip而不是使用子进程:

import pip

def install(package):

pip.main(['install',package])

# Example

if __name__ == '__main__':

try:

import pexpect

except ImportError:

install('pexpect')

import pexpect

另一个需要:

import pip

def import_with_auto_install(package):

try:

return __import__(package)

except ImportError:

pip.main(['install',package])

return __import__(package)

# Example

if __name__ == '__main__':

pexpect = import_with_auto_install('pexpect')

print(pexpect)

[编辑]

您应该考虑使用requirements.txt和pip.好像你正试图自动化部署(这很好!),在我的工具带中我还有virtualenvwrapper,vagrant和ansible.

这是我的输出:

(test)root@vagrant:~/test# pip uninstall pexpect

Uninstalling pexpect:

/usr/lib/python-environments/test/lib/python2.6/site-packages/ANSI.py

/usr/lib/python-environments/test/lib/python2.6/site-packages/ANSI.pyc

/usr/lib/python-environments/test/lib/python2.6/site-packages/FSM.py

/usr/lib/python-environments/test/lib/python2.6/site-packages/FSM.pyc

/usr/lib/python-environments/test/lib/python2.6/site-packages/fdpexpect.py

/usr/lib/python-environments/test/lib/python2.6/site-packages/fdpexpect.pyc

/usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect-2.4-py2.6.egg-info

/usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect.py

/usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect.pyc

/usr/lib/python-environments/test/lib/python2.6/site-packages/pxssh.py

/usr/lib/python-environments/test/lib/python2.6/site-packages/pxssh.pyc

/usr/lib/python-environments/test/lib/python2.6/site-packages/screen.py

/usr/lib/python-environments/test/lib/python2.6/site-packages/screen.pyc

Proceed (y/n)? y

Successfully uninstalled pexpect

(test)root@vagrant:~/test# python test.py

Downloading/unpacking pexpect

Downloading pexpect-2.4.tar.gz (113Kb): 113Kb downloaded

Running setup.py egg_info for package pexpect

Installing collected packages: pexpect

Running setup.py install for pexpect

Successfully installed pexpect

Cleaning up...



推荐阅读
author-avatar
沉醉在温柔箱
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有