简体   繁体   中英

Module is not found when trying to import it and running a virtual environment (venv)

I am currently using a virtual environment (venv) with python. The "venv" directory is inside my project folder by standard convention.

My project folder contains multiple different modules.

I have a jupyter notebook that has multiple import statements in the beginning. One of the import statements calls one of my other custom-made modules. This certain module imports tensorflow. When I call pip3 list -V in the terminal, I get:

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

However, when I try to import my module that contains tensorflow I get ModuleNotFoundError: No module named 'tensorflow' . We can clearly see that tensorflow has been installed by pip3. What's my problem here?

I am new to programming in general and would appreciate some guidance here. I have looked into the PYTHONPATH variable and am trying to understand it. I am also confused with this since I am implementing a virtual environment here. I am using MacOS.

You may be running your jupyter notebook from a different virtual environment. So if you are running jupyter from conda, you need to shut it down, and start jupyter server from terminal with your venv:

jupyter notebook

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM