【发布时间】:2019-06-19 11:58:53
【问题描述】:
我正在编写一个 python 安装程序脚本,需要通过脚本内的 pip 安装 2 个附加模块,然后导入并使用这些模块在同一个脚本中完成安装。 pip 调用工作正常,但是当我尝试导入刚安装的模块 (winshell) 时,我收到一个错误,它无法导入另一个模块 (win32con),它是我安装的第二个模块的一部分(pywin32).
如果我在出错后重新运行脚本,一切都会正常运行,所以我知道实际的 pip 安装工作正常。在重新运行程序之前,我似乎正在运行的 python 脚本不知道某些已安装的模块。有没有办法让正在运行的脚本“更新”它看到可用的模块而无需重新运行程序?
这里是简化的代码:
import os
import sys
try:
from pip import main as pipmain
except ImportError:
from pip._internal import main as pipmain
def create_shortcut():
print 'Creating shortcut...'
import winshell
link_filepath = os.path.join(winshell.desktop(), "Start.lnk")
with winshell.shortcut(link_filepath) as link:
link.path = sys.executable
link.description = "Shortcut to startup"
link.arguments = r"C:\temp\my_program.py"
def install_requirements():
print 'Installing requirements...'
pipmain(['install', '-r', 'wheelhouse/requirements.txt', '--no-index', '--find-links', 'wheelhouse'])
if __name__ == '__main__':
install_requirements()
create_shortcut()
这是错误:
C:\temp>python my_installer.py
Installing requirements...
Looking in links: wheelhouse
Collecting pywin32>=224 (from -r wheelhouse/requirements.txt (line 1))
Collecting winshell>=0.6 (from -r wheelhouse/requirements.txt (line 2))
Installing collected packages: pywin32, winshell
Successfully installed pywin32-224 winshell-0.6
Creating shortcut...
Traceback (most recent call last):
File "my_installer.py", line 24, in <module>
create_shortcut()
File "my_installer.py", line 10, in create_shortcut
import winshell
File "C:\Python27\lib\site-packages\winshell.py", line 30, in <module>
import win32con
ImportError: No module named win32con
当我第二次运行时(并且模块已经安装):
C:\temp>python my_installer.py
Installing requirements...
Looking in links: wheelhouse
Requirement already satisfied: pywin32>=224 in c:\python27\lib\site-packages (from -r wheelhouse/requirements.txt (line 1)) (224)
Requirement already satisfied: winshell>=0.6 in c:\python27\lib\site-packages (from -r wheelhouse/requirements.txt (line 2)) (0.6)
Creating shortcut...
C:\temp>
【问题讨论】:
-
你能提供更多关于你的代码的细节吗?以及您收到的确切错误消息!这将有助于更好地了解问题可能是什么
-
您可以编辑确切的错误吗?这个问题太模糊了
标签: python windows python-2.7 import pip