从 python 3.7 更新到 3.8 会导致 VS Code 中的 pylint 和其他模块出现问题

Update from python 3.7 to 3.8 causes problems with pylint and other modules in VS Code

我通过 linuxbrew 使用 python3,今天似乎有一个关键的 python-更新将 python 3.7 完全替换为 3.8。

至于我的 OS (Lubuntu 18.04 LTS):

Operating System: Ubuntu 18.04.4 LTS
Kernel: Linux 5.3.0-62-generic
Architecture: x86-64

这就是为什么我的所有 python 站点包的 pip 升级当时都不起作用的原因:

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U

抛出的错误是:

# /home/andylu/Desktop/Shell/python_package_complete_update_via_homebrew_n_pip: /home/linuxbrew/.linuxbrew/bin/pip: /home/linuxbrew/.linuxbrew/opt/python/bin/python3.7: bad interpreter: No such file or directory
# WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
# Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
# To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
# Defaulting to user installation because normal site-packages is not writeable
# ERROR: You must give at least one requirement to install (see "pip help install")

后来,我发现有一个新的python版本安装。 奇怪的是,创建了两个文件夹:

  1. /home/linuxbrew/.linuxbrew/lib/python3.8
  2. /home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.3_2/lib/python3.8

由于新 python3.8 文件夹中的站点包非常空,我将所有站点包(> 1GB)从 remnant/residual python3.7 移动到新 python3.8 文件夹。 有了这个,我假设它们在 python 3.8 下也能正常工作,而不是删除并重新安装它们。

此外,我认为 /home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.3_2/lib/python3.8 中的站点包似乎只是 /home/linuxbrew/.linuxbrew/lib/python3.8.

下文件夹的 link

然后我将 pip package-update 命令更新为以下内容:

python3 -m pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 python3 -m pip install -U

这似乎有效。 然而,在打开 VS Code 时,它​​总是在右下角显示“pylint linter 的路径无效”,即使该路径存在:

USER:~/Desktop/Python/Scripts$ which pylint
/home/linuxbrew/.linuxbrew/bin/pylint

USER:~/Desktop/Python/Scripts$ which python
/usr/bin/python

USER:~/Desktop/Python/Scripts$ which python3
/home/linuxbrew/.linuxbrew/bin/python3

接下来,通过 Ctrl + Alt + P 在 VS Code 中执行 python-script 时,出现以下错误(以及整个输出):

USER:~/Desktop/Python/Scripts$

/home/linuxbrew/.linuxbrew/bin/python3 -c "from IPython import start_ipython; start_ipython()" /home/cripts/Master/Import_export/AERMET/import_and_manip_sitespec_meteodata_AERMET.py
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages/numpy/core/__init__.py in <module>
     21 try:
---> 22     from . import multiarray
     23 except ImportError as exc:

/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages/numpy/core/multiarray.py in <module>
     11 
---> 12 from . import overrides
     13 from . import _multiarray_umath

/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages/numpy/core/overrides.py in <module>
      6 
----> 7 from numpy.core._multiarray_umath import (
      8     add_docstring, implement_array_function, _get_implementing_args)

ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
~/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/Master/Import_export/AERMET/import_and_manip_sitespec_meteodata_AERMET.py in <module>
     40 
     41 import re
---> 42 import numpy as np
     43 import pandas as pd
     44 from tqdm import tqdm

/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages/numpy/__init__.py in <module>
    138     from . import _distributor_init
    139 
--> 140     from . import core
    141     from .core import *
    142     from . import compat

/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages/numpy/core/__init__.py in <module>
     46 """ % (sys.version_info[0], sys.version_info[1], sys.executable,
     47         __version__, exc)
---> 48     raise ImportError(msg)
     49 finally:
     50     for envkey in env_added:

ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/home/linuxbrew/.linuxbrew/opt/python@3.8/bin/python3.8"
  * The NumPy version is: "1.19.0"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy.core._multiarray_umath'

不过,numpy 已经是最新的了:

USER:~/Desktop/Python/Scripts$ pip install numpy
Requirement already satisfied: numpy in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (1.19.0)

此外,在 VS Codesettings.json 文件中,我声明如下:

"python.autoComplete.extraPaths": [
        "/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts",
        "/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages",
        "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.3_2/lib/python3.8/site-packages"
    ]

在我的 .bashrc - 文件中,我已经更新了所有内容但无济于事:

# Link to own and official python scripts and packages
export PYTHONPATH=/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts:/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages:/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.3_2/lib/python3.8/site-packages

# Homebrew
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"

所有这些问题让我完全措手不及,我感到完全迷失了。 我今天想继续我的项目,但是在互联网上已经花了 2 个小时发现 none 提议的解决方案对我有用,因此我在这里开始这个问题。

解决上述问题的方法是,每个 module/package 引起问题的解决方法都需要先卸载,然后从头开始重新安装。

这显然不同于简单地尝试更新已错误安装的软件包。

我推测这是通过手动将安装在 /../python3.7/site-packages 中的 python 站点包移动到新的 /../python3.8/site-packages 目录引起的。 显然,并不是所有的软件包之后都能正常工作,需要卸载并重新安装而不是仅仅更新。

更详细地说,我做了 pip uninstall packagename 然后 pip install packagename 每个包都引起了问题,幸运的是少于 10 个。

除了使用 pip install ... 之外,还可以使用 python3 -m pip install ... 如果效果更好或者 pip-binary 以某种方式损坏,因为它最初是我的情况(见上面的问题)。

通过上述变通方法解决问题后,自然会出现 关于如何更优雅地将站点包迁移到新 python 3.x 版本的问题。