Python 在 Windows 上使用 Tensorflow 与 Anaconda 和 PyCharm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44059934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:41:46  来源:igfitidea点击:

using Tensorflow with Anaconda and PyCharm on Windows

pythontensorflowanaconda

提问by user495236

I am using Anaconda 4.3.1 64 Bit on Windows 10 64 Bit. I have successfully installed Tensorflow (CPU) according to this. The test program runs in the commandline, but not in PyCharm. The problem is that it works only with activate tensorflow.

我在 Windows 10 64 位上使用 Anaconda 4.3.1 64 位。我已经根据this成功安装了Tensorflow(CPU)。测试程序在命令行中运行,但不在PyCharm 中。问题是它只适用于activate tensorflow

I followed the steps from Pycharm anaconda import tensor flow library issue("You need to do these following steps:"). It seems now that tensorflow is loaded, but I get a new error:

我按照Pycharm anaconda 导入张量流库问题中的步骤操作(“您需要执行以下步骤:”)。现在似乎已加载 tensorflow,但我收到一个新错误:

ImportError: Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy. If you're working with a numpy git repo, try git clean -xdf(removes all files not under version control). Otherwise reinstall numpy.

导入错误:导入多阵列 numpy 扩展模块失败。很可能您正在尝试导入失败的 numpy 构建。如果您正在使用 numpy git repo,请尝试git clean -xdf(删除所有不受版本控制的文件)。否则重新安装numpy。

How can I use TensorFlow in PyCharm? (BTW: Tensorflow works with PyCharm on my Fedora VM.)

如何在 PyCharm 中使用 TensorFlow?(顺便说一句:Tensorflow 在我的 Fedora VM 上与 PyCharm 一起工作。)

Edit:

编辑:

Can I use TensorFlow with Jupyter? When running "Jupyter (tensorflow)" from the Anaconda menu, a console window opens and immediately closes.

我可以将 TensorFlow 与 Jupyter 一起使用吗?从 Anaconda 菜单运行“Jupyter (tensorflow)”时,控制台窗口会打开并立即关闭。

采纳答案by PdevG

When PyCharm is open: if you navigate to preferences: Project: Project Interpreter. You can there either create your own virtualenv (and then manually install the required packages) or make sure you have selected the anaconda python interpreter for your project.

当 PyCharm 打开时:如果您导航到首选项:项目:项目解释器。您可以在那里创建自己的 virtualenv(然后手动安装所需的包)或确保您已为您的项目选择了 anaconda python 解释器。

回答by klimkin

If following official instructions, you have created a virtual environment called tensorflow. The environment is located in Anaconda3\envs\tensorflowdirectory, where Anaconda3is the Anaconda installation directory. You just need to point PyCharm to the python.exewhich is located there.

如果遵循官方说明,您已经创建了一个名为tensorflow的虚拟环境。环境位于Anaconda3\envs\tensorflow目录中,其中Anaconda3是Anaconda的安装目录。您只需要将 PyCharm 指向python.exe位于那里的 。

The settings path in PyCharm is something like this:

PyCharm 中的设置路径是这样的:

Settings->Project Interpreter->Add Local->Virtualenv Environment->Existing environment

Settings->Project Interpreter->Add Local->Virtualenv Environment->Existing environment

Point the interpreter to the python.exein the Anaconda3\envs\tensorflowdirectory.

点解释到python.exeAnaconda3\envs\tensorflow目录中。

回答by onur güng?r

I recommend installing babunand creating a virtualenvwith virtualenvwrapperthere and install on that environment.

我建议在那里安装babun和创建一个并安装在该环境中。virtualenvvirtualenvwrapper

After doing this, just choose the pythonbinary in the relevant directory of the virtualenv you created, i.e. ~/.virtualenvs/myenv/bin/pythonor ~/.virtaulenvs/myenv/usr/bin/python

这样做后,只需python在您创建的virtualenv的相关目录中选择二进制文件,即~/.virtualenvs/myenv/bin/python~/.virtaulenvs/myenv/usr/bin/python

回答by MaxB

Before importing anything else, do this

在导入其他任何东西之前,请执行此操作

import sys
print(sys.path)

import os
print(os.environ)
print(os.environ['CUDA_VISIBLE_DEVICES'])

from the command line (when TF works) and from PyCharm (when it doesn't). If you see any relevant differences, adjust accordingly (define the environment variables in PyCharm, etc.)

从命令行(当 TF 工作时)和来自 PyCharm(当它不工作时)。如果您看到任何相关差异,请相应调整(在 PyCharm 中定义环境变量等)