【问题标题】:Module is not found when trying to import it and running a virtual environment (venv)尝试导入模块并运行虚拟环境 (venv) 时找不到模块
【发布时间】:2021-08-16 19:50:28
【问题描述】:

我目前正在使用带有 python 的虚拟环境 (venv)。按照标准约定,“venv”目录位于我的项目文件夹中。

我的项目文件夹包含多个不同的模块。

我有一个 jupyter 笔记本,开头有多个导入语句。导入语句之一调用我的其他定制模块之一。这个特定的模块导入 tensorflow。当我在终端中调用pip3 list -V 时,我得到:

Package                 Version
----------------------- ---------
absl-py                 0.13.0
anyio                   3.3.0
appnope                 0.1.2
argon2-cffi             20.1.0
astunparse              1.6.3
attrs                   21.2.0
autopep8                1.5.7
Babel                   2.9.1
backcall                0.2.0
bleach                  4.0.0
cachetools              4.2.2
certifi                 2021.5.30
cffi                    1.14.6
charset-normalizer      2.0.4
cycler                  0.10.0
debugpy                 1.4.1
decorator               5.0.9
defusedxml              0.7.1
entrypoints             0.3
gast                    0.3.3
google-auth             1.34.0
google-auth-oauthlib    0.4.5
google-pasta            0.2.0
grpcio                  1.39.0
h5py                    2.10.0
idna                    3.2
ipykernel               6.2.0
ipython                 7.26.0
ipython-genutils        0.2.0
ipywidgets              7.6.3
jedi                    0.18.0
Jinja2                  3.0.1
json5                   0.9.6
jsonschema              3.2.0
jupyter-client          6.1.12
jupyter-core            4.7.1
jupyter-server          1.10.2
jupyterlab              3.1.7
jupyterlab-pygments     0.1.2
jupyterlab-server       2.7.0
jupyterlab-widgets      1.0.0
Keras-Preprocessing     1.1.2
kiwisolver              1.3.1
Markdown                3.3.4
MarkupSafe              2.0.1
matplotlib              3.4.3
matplotlib-inline       0.1.2
mistune                 0.8.4
nbclassic               0.3.1
nbclient                0.5.4
nbconvert               6.1.0
nbformat                5.1.3
nest-asyncio            1.5.1
notebook                6.4.3
numpy                   1.18.5
oauthlib                3.1.1
opt-einsum              3.3.0
packaging               21.0
pandocfilters           1.4.3
parso                   0.8.2
pexpect                 4.8.0
pickleshare             0.7.5
Pillow                  8.3.1
pip                     21.2.4
prometheus-client       0.11.0
prompt-toolkit          3.0.19
protobuf                3.17.3
ptyprocess              0.7.0
pyasn1                  0.4.8
pyasn1-modules          0.2.8
pycodestyle             2.7.0
pycparser               2.20
Pygments                2.10.0
pyparsing               2.4.7
pyrsistent              0.18.0
python-dateutil         2.8.2
pytz                    2021.1
pyzmq                   22.2.1
requests                2.26.0
requests-oauthlib       1.3.0
requests-unixsocket     0.2.0
rsa                     4.7.2
scipy                   1.4.1
Send2Trash              1.8.0
setuptools              49.2.1
six                     1.16.0
sniffio                 1.2.0
tensorboard             2.6.0
tensorboard-data-server 0.6.1
tensorboard-plugin-wit  1.8.0
tensorflow              2.3.0
tensorflow-estimator    2.3.0
termcolor               1.1.0
terminado               0.11.0
testpath                0.5.0
toml                    0.10.2
tornado                 6.1
traitlets               5.0.5
urllib3                 1.26.6
wcwidth                 0.2.5
webencodings            0.5.1
websocket-client        1.2.1
Werkzeug                2.0.1
wheel                   0.37.0
widgetsnbextension      3.5.1
wrapt                   1.12.1

但是,当我尝试导入包含 tensorflow 的模块时,我得到了ModuleNotFoundError: No module named 'tensorflow'。我们可以清楚的看到 tensorflow 已经被 pip3 安装好了。我的问题是什么?

一般来说,我是编程新手,希望能得到一些指导。我研究了 PYTHONPATH 变量并试图理解它。我也对此感到困惑,因为我在这里实现了一个虚拟环境。我正在使用 MacOS。

【问题讨论】:

    标签: python module python-import importerror python-venv


    【解决方案1】:

    您可能在不同的虚拟环境中运行您的 jupyter notebook。因此,如果您从 conda 运行 jupyter,则需要将其关闭,然后使用 venv 从终端启动 jupyter 服务器:

    jupyter notebook
    

    【讨论】:

    • 是的,这就是我一直在做的。
    • 所以你有 2 个选项要做:1. 安装 tensorflow 和 conda 环境所需的所有模块 (conda install tensorflow) 2. 关闭 conda,终止 jupyter 并从你的 venv 运行一个 jupyter 服务器跨度>
    • 我没有使用 conda。我正在使用venv。我也不能在这个项目中使用 conda,因为我特别需要 tensorflow 2.3,而 conda 在他们的包 repo 中没有它。
    • 如果你只是在笔记本中运行import tensorflow,会不会出现同样的错误?
    • 你确定你在你的venv中安装了包吗?安装前有没有激活venv?如果是,那么你确定你是从这个激活的环境中运行 jupyter 吗?
    猜你喜欢
    • 2020-06-21
    • 2021-07-04
    • 2019-03-13
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    相关资源
    最近更新 更多