Tensorflow package is not found when running python program

huangapple go评论66阅读模式
英文:

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&#39;Hi, {name}&#39;)  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == &#39;__main__&#39;:
    print_hi(&#39;PyCharm&#39;)

When I run it, it gives me the error:

Traceback (most recent call last):
  File &quot;C:\Users\max\PycharmProjects\pythonProject\main.py&quot;, line 2, in &lt;module&gt;
    from tensorflow.keras import layers, models, optimizers, utils, datasets
ModuleNotFoundError: No module named &#39;tensorflow&#39;

But the package is installed, I can see it here in the virtual env:

Tensorflow package is not found when running python program

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.

Tensorflow package is not found when running python program

答案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\&lt;user&gt;\AppData\Local\Programs\Python\Python&lt;version&gt; 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.

huangapple
  • 本文由 发表于 2023年6月8日 18:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定