简体   繁体   English

无法在pyCharm中导入随Anaconda安装的文件:是否需要将Anaconda添加到Python PATH?

[英]Cannot import file installed with Anaconda in pyCharm: Need to add anaconda to Python PATH?

I am a newbie in Python and I wanted to run some reinforcement learning algorithms with Python using AI Gym: https://github.com/openai/gym 我是Python的新手,我想使用AI Gym使用Python运行一些强化学习算法: https//github.com/openai/gym

I want to use tensorflow so I made a python 3.5 environment called "tensorflow" with Anaconda and installed it there. 我想使用tensorflow,所以我用Anaconda创建了一个名为“ tensorflow”的python 3.5环境,并将其安装在那里。 In the same environment, I installed AIgym using 在同一环境中,我使用以下命令安装了AIgym:

pip install gym 点安装健身房

from the Anaconda prompt. 从Anaconda提示符下。 With

conda list 康达清单

I can see "gym" as a package installed in that environment, but in Anaconda Navigator it is not listed (neither in the "tensofrflow" environment or anywhere else. 我可以看到“ gym”是在该环境中安装的软件包,但是在Anaconda Navigator中并未列出(既不在“ tensofrflow”环境中,也未在其他任何地方)。

Anyhow if I try to do 无论如何,如果我尝试去做

import gym 进口体育馆

I get 我懂了

No module named 'gym' 没有名为“ gym”的模块

I am sure I have the Anaconda interpreter selected in the project configurations. 我确定我在项目配置中选择了Anaconda解释器。 I also tried 我也试过

pip install gym 点安装健身房

from the regular command-line prompt but it still would refuse to find 'gym'. 从常规命令行提示符处输入,但它仍然拒绝找到“ gym”。

One suggestion was to include the Anaconda path to the system's Python PATH. 一个建议是包括系统的Python PATH的Anaconda路径。 I am not sure how to do this correctly. 我不确定如何正确执行此操作。 Most advice is for Linux and I use Windows 10. Is it the path of the python.exe inside the anaconda folder? 大多数建议是针对Linux的,而我使用Windows10。它是anaconda文件夹中python.exe的路径吗? I've seen people trying 我见过有人在尝试

export PATH=C:/path/anaconda:$PATH 导出PATH = C:/ path / anaconda:$ PATH

but export is not recognized for me in any command prompt. 但在任何命令提示符下都无法识别导出。

which python 哪个蟒蛇

is also not recognized in order to check which python is being used. 也无法识别以检查正在使用哪个python。 I also tried adding the anaconda path to the system path (This PC > Advanced > Environment Variables > System Variables > Path) but no dice. 我还尝试将anaconda路径添加到系统路径(此PC>高级>环境变量>系统变量>路径),但没有骰子。

Any suggestions would be appreciated. 任何建议,将不胜感激。

--Update 1-- -更新1--

Thomas, thanks for your answer. 托马斯,谢谢你的回答。 I have tried what you suggested - tried importing gym from the python in-line interpreter from within the anaconda prompt/environment, and it worked! 我尝试了您的建议-尝试从anaconda提示/环境中的python嵌入式解释器中导入健身房,并且它起作用了!

cmd CMD CMD

In pyCharm, though, even while I am sure I have the Anaconda interpreter selected for the project, it doesn't work. 尽管在pyCharm中,即使我确定已经为该项目选择了Anaconda解释器,但它仍然无法正常工作。

PyCharm PyCharm pyCharm

--Update 2-- I can see my envs and path but how exactly do you add an env in pyCharm? -更新2--我可以看到我的环境和路径,但是如何在pyCharm中添加环境呢? The method I have been using was, configure the project to use the Anaconda interpreter and switch environments from the anaconda prompt. 我一直在使用的方法是,配置项目以使用Anaconda解释器并从anaconda提示符下切换环境。 A search gives the same answer. 搜索给出相同的答案。

env_advice

It is strange because I have tensorflow installed only in my tensorflow environment, and tensorflow itself imports without problem, as you can see here: 奇怪是因为我仅在我的tensorflow环境中安装了tensorflow,并且tensorflow本身导入没有问题,如您在此处看到的:

tf_import

Thus the tensorflow environment must be active. 因此,张量流环境必须处于活动状态。

Thanks in advance! 提前致谢!

In your cmd -example, you're using an environment called tensorflow , and not the "root" (aka default) environment. 在您的cmd -example中,您使用的是称为tensorflow的环境,而不是“ root”(也称为默认)环境。 You can add that same environment to PyCharm, and use that instead. 您可以将相同的环境添加到PyCharm中,然后使用

If you're unsure of where the environment is located you can use the conda command conda env list , ie: 如果不确定环境的位置,可以使用conda命令conda env list ,即:

(tensorflow) C:\Users\Riel> conda env list

That will print a list of the installed Anaconda environments and their locations. 这将打印已安装的Anaconda环境及其位置的列表。 Find your tensorflow environment and add it to PyCharm. 查找您的tensorflow环境并将其添加到PyCharm。

When I do it on my system, I get this output: 当我在系统上执行此操作时,将得到以下输出:

# conda environments:
#
benv                     E:\Anaconda3\envs\benv
game                     E:\Anaconda3\envs\game
ml                       E:\Anaconda3\envs\ml
py26                     E:\Anaconda3\envs\py26
py27                     E:\Anaconda3\envs\py27
py33                     E:\Anaconda3\envs\py33
py34                     E:\Anaconda3\envs\py34
py35                     E:\Anaconda3\envs\py35
py36                     E:\Anaconda3\envs\py36
root                  *  E:\Anaconda3

Adding environment to PyCharm 为PyCharm添加环境

To add the environment to PyCharm , select from the menu bar: 要将环境添加到PyCharm ,请从菜单栏中选择:

File -> Settings .. -> Project Interpreter -> Click the cog wheel -> Add Local 文件 -> 设置 ..-> 项目解释器 ->单击齿轮-> 添加本地

Then find the folder of your tensorflow environment and the python.exe -file in it and add it, which according to your image, is G:\\Programs\\Anaconda\\envs\\tensorflow\\python.exe 然后找到您的tensorflow环境的文件夹和其中的python.exe -file并将其添加,根据您的图像,它是G:\\ Programs \\ Anaconda \\ envs \\ tensorflow \\ python.exe

(See image below) (见下图)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 安装了Anaconda,在Python(Pycharm)中导入numpy - Getting numpy to import in Python (Pycharm), Anaconda installed 在 anaconda 环境中安装了 tensorflow 但无法导入(Tensorflow 1.15、Python 3.6、PyCharm) - Installed tensorflow in an anaconda env but cannot import (Tensorflow 1.15, Python 3.6, PyCharm) 无法导入安装在PyCharm中的anaconda中的软件包 - Can not import package installed in anaconda in PyCharm 无法将gdal导入到安装了anaconda(MacOSX)的python中 - Unable import gdal into python installed with anaconda (MacOSX) 用 Anaconda 安装了一个包,无法在 Python 中导入 - Installed a package with Anaconda, can't import in Python 将已安装的 CPLEX 导入 Anaconda - Import installed CPLEX into Anaconda 无法导入 C:\\Users\\username\\Anaconda3\\python.exe 时如何访问 Pycharm 中的 Anaconda 基础环境? - How to access the Anaconda base environment in Pycharm when I cannot import C:\Users\username\Anaconda3\python.exe? 已构建和安装Python Anaconda软件包,但无法导入 - Python Anaconda package built and installed, but cannot be imported 无法在Windows上使用Anaconda Python Stack导入MySQLdb - Cannot import MySQLdb on Windows with Anaconda Python Stack 无法使用Anaconda-Python导入scitools - Cannot import scitools using Anaconda-Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM