用 Anaconda 安装了一个包,无法在 Python 中导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43485569/
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:08:08  来源:igfitidea点击:

Installed a package with Anaconda, can't import in Python

pythonpython-3.xpackageinstallanaconda

提问by KDogg

Forgive me but I'm new to python. I've installed a package (theano) using conda install theano, and when I type conda list, the package exists

原谅我,但我是 python 的新手。我已经使用 安装了一个包 (theano) conda install theano,当我输入时conda list,该包存在

However, when I enter the python interpreter by running python, and try to import it with import theano, I get an error: "no module named theano", and when I list all python modules, theano doesn't exist.

但是,当我通过运行进入 python 解释器python并尝试使用 导入它时import theano,出现错误:“没有名为 theano 的模块”,并且当我列出所有 python 模块时,theano 不存在。

What am I missing?

我错过了什么?

采纳答案by mataney

Probably due to the fact you have multiply python envs installed in your computer. when you do which pythonyou will probably get the native python installed in your computer. that is /usr/bin/python

可能是因为您的计算机中安装了多个 python 环境。当您这样做时,您which python可能会在您的计算机中安装本机python。那是/usr/bin/python

You want to use the Python that came when you installed Anaconda. Just add Anaconda path to the beginning of your $PATH. (In order to do this you probably need to edit your ~/.bashrcfile (or the equivalent file for your shell) then source ~/.bashrc.

您想使用安装 Anaconda 时附带的 Python。只需将 Anaconda 路径添加到$PATH. (为了做到这一点,您可能需要编辑您的~/.bashrc文件(或您的 shell 的等效文件),然后source ~/.bashrc.

Next time you will go to will run pythonand import theanoyou'll succeed.

下次你去会跑pythonimport theano你会成功。

回答by James Callender

When I had this issue my python install was actually missing a "site-packages" path reference. To solve/workaround the issue do the following.

当我遇到这个问题时,我的 python 安装实际上缺少“站点包”路径引用。要解决/解决该问题,请执行以下操作。

  1. Search for your newly installed package from the Anaconda directory and note the path. (e.g. C:\Anaconda\site-packages)
  2. Run the following in your terminal:
  1. 从 Anaconda 目录中搜索新安装的软件包并记下路径。(例如 C:\Anaconda\site-packages)
  2. 在终端中运行以下命令:
        python -c "import site; print(site.getsitepackages())"

Example Output: ['C:\Anaconda3', 'C:\Anaconda3\lib\site-packages']

示例输出:['C:\Anaconda3', 'C:\Anaconda3\lib\site-packages']

  1. If the path noted in step one is missing from the list then that's your problem. The quick fix is to move the new package to a listed site-packages folder or add the missing path to your PYTHONPATH environment variable.
  1. 如果列表中缺少第一步中记录的路径,那么这就是您的问题。快速修复是将新包移动到列出的 site-packages 文件夹或将缺少的路径添加到 PYTHONPATH 环境变量。

If you're interested in managing your own "site-packages" locations check out the Python Docfor details on setting up a site config file.

如果您对管理自己的“站点包”位置感兴趣,请查看Python 文档,了解有关设置站点配置文件的详细信息。

回答by ally-e

Do you have another installation of Python on your system? You can run "which python" in your terminal to determine which Python will be used.

您的系统上是否安装了另一个 Python?您可以在终端中运行“which python”来确定将使用哪个 Python。

回答by Chris A

So I also had the same problem, turn out that I had named my own file to the same modulename (graphviz) and it tried to import that one instead... Took me a while before I figured that one out!

所以我也遇到了同样的问题,原来我已经将我自己的文件命名为相同的模块名(graphviz),它试图导入那个……我花了一段时间才弄明白!

回答by emeralddove

I had a base environment where I had installed keras_vggface using conda (sudo pip install git+https://github.com/rcmalli/keras-vggface.git: Courtesy: https://machinelearningmastery.com/how-to-perform-face-recognition-with-vggface2-convolutional-neural-network-in-keras/). Launched anaconda-navigator from base (post conda activate base), import keras_vggface failed.

我有一个使用 conda 安装 keras_vggface 的基本环境(sudo pip install git+ https://github.com/rcmalli/keras-vggface.git:礼貌:https: //machinelearningmastery.com/how-to-perform-face -recognition-with-vggface2-convolutional-neural-network-in-keras/)。从 base (post conda activate base)启动 anaconda-navigator ,导入 keras_vggface 失败。

When baseis deactivated, and in python command line, import worked fine. which pythonreveals the one within anaconda bin directory. Now, I did pip3 install keras_vggfacewhile being in base.

何时base停用,在 python 命令行中,导入工作正常。which python显示 anaconda bin 目录中的一个。现在,我pip3 install keras_vggfacebase.

Now, I am able to import the module from within baseand in python prompt and also from jupyter notebook launched from base via anaconda-navigator.

现在,我可以从basepython 提示符内部和中导入模块,也可以从通过 anaconda-navigator 从 base 启动的 jupyter notebook导入模块。

Note: this is not an expert advice on how it has to be done; please use this experience with pinch of salt.

注意:这不是关于如何完成的专家建议;请用少许盐来使用这种经验。

回答by agorapotatoes

I had this problem and realised that the issue was that ipythonand jupyter-notebookdid not have the same sys.pathas python, just in case that helps anyone.

我有这个问题,并意识到这个问题是ipythonjupyter-notebook没有一样sys.pathpython,以防万一,可以帮助任何人。