【问题标题】:Pycharm virtualenv stops working suddenly as contextlib module goes missing由于 contextlib 模块丢失,Pycharm virtualenv 突然停止工作
【发布时间】:2020-05-27 20:52:07
【问题描述】:

Pycharm (Win10) 突然停止运行并出现以下错误

..\env\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 64065 --file start_file.py 
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\pydevd.py", line 7, in <module>
    from contextlib import contextmanager
ImportError: No module named 'contextlib'

Process finished with exit code 1

我尝试在虚拟环境中导入 contextlib,但是:

...\venv>Scripts\activate

(venv) ...\venv>pip install contextlib
ERROR: Could not find a version that satisfies the requirement contextlib (from versions: none)
ERROR: No matching distribution found for contextlib
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

我该如何解决这个问题?

我可以在 win explorer 的 Lib 模块中看到 contextlib:

我升级了 pip,但是

venv>pip install contextlib
ERROR: Could not find a version that satisfies the requirement contextlib (from versions: none)
ERROR: No matching distribution found for contextlib

venv>pip install 'contextlib==3.8'
ERROR: Invalid requirement: "'contextlib==3.8'"

在导入时我应该为 contextlib 指定任何版本吗?

【问题讨论】:

  • 你的 venv 是什么 python 版本?从屏幕截图中,您似乎签入了与 venv 不同的本地 python 安装。
  • 似乎 venv 版本正确 -- venv>python --version Python 3.8.0
  • 你最近更新了 Python 吗?如果是,那么有很多事情可能会出错。尝试删除您的 venv 并创建一个新的
  • venv 版本是 3.8 - python 也是 3.8

标签: python pycharm virtualenv virtual-environment


【解决方案1】:

谢谢@hurlenko。

不确定是什么问题,但以下解决了它:

- Install new venv using desired Python executable
- Try and install the requirements within custom requirements file
- Use the venv and create debug config in Pycharm
- Run the new config within Pycharm
- Got dependency errors - install dependency (using Pycharm as it is way easier)
- Got new dependency errors due to installation in previous step - installed the newly failing dependencies
- Repeat the above step until all dependencies are resolved

之后我就可以毫无问题地运行了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-23
    • 2017-08-22
    • 2021-02-01
    • 2016-12-29
    • 2018-05-09
    • 2015-12-07
    • 2015-08-23
    相关资源
    最近更新 更多