1

这是我的代码:

import os

try:
    import winshell
except:
    os.system('python -m pip  install --upgrade pip')
    os.system('pip install pywin32')
    os.system('pip install winshell')
    import winshell

print('hello world')

我想导入winshell模块,当它没有安装时,安装它然后再次导入。

但我有一个错误

Requirement already up-to-date: pip in c:\users\mini\appdata\local\programs\python\python37-32\lib\site-packages (18.1)
Collecting pywin32
  Using cached https://files.pythonhosted.org/packages/8a/37/917c4020e93e0e854d4cbff1cbdf14ec45a6d1cedf52f8cafdea5b22451a/pywin32-224-cp37-cp37m-win32.whl
Installing collected packages: pywin32
Successfully installed pywin32-224
Collecting winshell
  Using cached https://files.pythonhosted.org/packages/91/3f/d23d248029a49042d278af9c86b53bc90845e3739a0d7493e00cbac0e4cd/winshell-0.6.zip
Installing collected packages: winshell
  Running setup.py install for winshell: started
    Running setup.py install for winshell: finished with status 'done'
Successfully installed winshell-0.6
Traceback (most recent call last):
  File "D:\share\test.py", line 4, in <module>
    import winshell
ModuleNotFoundError: No module named 'winshell'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\share\test.py", line 9, in <module>
    import winshell
  File "C:\Users\mini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\winshell.py", line 30, in <module>
    import win32con
ModuleNotFoundError: No module named 'win32con'
[Finished in 13.6s with exit code 1]
[shell_cmd: python -u "D:\share\test.py"]
[dir: D:\share]
[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Users\mini\AppData\Local\Programs\Python\Python37-32\Scripts\;C:\Users\mini\AppData\Local\Programs\Python\Python37-32\]

该脚本安装了 pywin32 和 winshell,但随后,它说没有名为 winshell 的模块,我必须再次尝试此脚本。

我尝试了另一个模块

import os

try:
    import requests
except:
    os.system('python -m pip  install --upgrade pip')
    os.system('pip install requests')
    import requests

print('hello world')

它运作良好

Requirement already up-to-date: pip in c:\users\mini\appdata\local\programs\python\python37-32\lib\site-packages (18.1)
Collecting requests
  Downloading https://files.pythonhosted.org/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl (91kB)
Collecting urllib3<1.24,>=1.21.1 (from requests)
  Downloading https://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl (133kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting idna<2.8,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/df/f7/04fee6ac349e915b82171f8e23cee63644d83663b34c539f7a09aed18f9e/certifi-2018.8.24-py2.py3-none-any.whl (147kB)
Installing collected packages: urllib3, chardet, idna, certifi, requests
Successfully installed certifi-2018.8.24 chardet-3.0.4 idna-2.7 requests-2.19.1 urllib3-1.23
hello world
[Finished in 10.5s]

然后我很困惑,为什么第一个脚本需要运行两次,而第二个脚本只需要运行一次?

4

0 回答 0