英文:
Tensorflow package is not found when running python program
问题
我有一个通过PyCharm生成的超级简单的Python设置:
import numpy as np
from tensorflow.keras import layers, models, optimizers, utils, datasets
def print_hi(name):
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
当我运行它时,它给我一个错误:
Traceback (most recent call last):
File "C:\Users\max\PycharmProjects\pythonProject\main.py", line 2, in <module>
from tensorflow.keras import layers, models, optimizers, utils, datasets
ModuleNotFoundError: No module named 'tensorflow'
但是该包已安装,我可以在虚拟环境中看到它:
[![在这里输入图片描述][1]][1]
我怀疑它可能正在全局环境中查找包,而不是虚拟环境。如何检查呢?
我看到以下解释器。尝试选择两者,但都没有成功。
[![在这里输入图片描述][2]][2]
英文:
I have a super simple Python setup generated through PyCharm:
import numpy as np
from tensorflow.keras import layers, models, optimizers, utils, datasets
def print_hi(name):
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
When I run it, it gives me the error:
Traceback (most recent call last):
File "C:\Users\max\PycharmProjects\pythonProject\main.py", line 2, in <module>
from tensorflow.keras import layers, models, optimizers, utils, datasets
ModuleNotFoundError: No module named 'tensorflow'
But the package is installed, I can see it here in the virtual env:
I suspect it might be looking for the package in the global env, not virtual env. How to check that?
I see the following interpreters. Tried selecting both, none worked.
答案1
得分: 1
请查看此答案中的步骤:https://stackoverflow.com/a/66016514/9714255。
它通过PyCharm下载pandas
模块,但对于tensorflow
也是一样的。
您可以拥有任意数量的Python解释器。每个解释器都存储在PyCharm中,路径为C:\Users\<user>\AppData\Local\Programs\Python\Python<version>
,您可以在Lib
文件夹中找到已安装的包(首先按照上述路径)。每个解释器都有自己的包,当您下载tensorflow
(就像上面一样)时,它会作为此解释器中的一个包添加,因此您可以使用它。您可以拥有许多具有不同包的解释器。
我不确定,但我也认为Python解释器对应于您使用的Python版本。
此链接可能会有所帮助:https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#interpreter。
英文:
Check the procedure in this answer: https://stackoverflow.com/a/66016514/9714255.
It downloads the module through PyCharm for pandas
but it's the same for tensorflow
.
You can have as many Python interpreters you want. Each one is stored through PyCharm, in the path C:\Users\<user>\AppData\Local\Programs\Python\Python<version>
and you can find your installed packages in the Lib
folder (following the previous path first). Each interpreter has its own packages, and when you downloaded tensorflow
(like above), it was added as a package in this interpreter, and so you are able to use it. You can have many interpreters with different packages in each one.
I am not sure, but I also think python interpreters correspond to the python version you use.
This link might be helpful: https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#interpreter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论