简体   繁体   English

Conda 安装的软件包,Python 找不到它

[英]Package installed by Conda, Python cannot find it

I try to install Theano by Anaconda.我尝试通过 Anaconda 安装 Theano。 It works, but when I enter the python -i, import theano shows No module named 'theano' .它有效,但是当我输入 python -i 时, import theano显示No module named 'theano' Do I need to switch another interpreter of Python, how?我是否需要切换另一个 Python 解释器,如何切换? Also, for the packages installed by conda, if I don't double install them, can I find in Python?另外,对于conda安装的包,如果我不双重安装它们,我可以在Python中找到吗? How is Python related to Python by Anaconda? Python 与 Anaconda 的 Python 有什么关系? Thanks!!!谢谢!!!

I had have a similar issue, trying to install folium .我有一个类似的问题,试图安装folium If you are using the Anaconda :如果您使用的是Anaconda

When you install using conda install -c conda-forge folium , the package will be placed in:当您使用conda install -c conda-forge folium ,软件包将被放置在:

./anaconda3/envs/[name env]/lib/python3.7/site-packages/folium

When you install using pip (with a anaconda env activated), pip install folium , the package will be placed in:当您使用 pip(激活 anaconda env)进行pip install foliumpip install folium ,该包将被放置在:

./anaconda3/lib/python3.7/site-packages/folium

Python use first the sites-packages as the target directory of manually built python packages. Python首先使用sites-package作为手动构建的python包的目标目录。 When you build and install python packages from source (using distutils, probably by executing python setup.py install ), you will find the installed modules in site-packages by default.当您从源代码构建和安装 python 包时(使用 distutils,可能通过执行 python setup.py install ),默认情况下您会在站点包中找到已安装的模块。

In this case you have two places: /anaconda3/lib/python3.7/site-packages/ and /anaconda3/envs/[name env]/lib/python3.7/site-packages/ .在这种情况下,您有两个位置: /anaconda3/lib/python3.7/site-packages//anaconda3/envs/[name env]/lib/python3.7/site-packages/

First the modules will be available as default in /anaconda3/lib/python3.7/site-packages/ .首先,模块将在/anaconda3/lib/python3.7/site-packages/默认/anaconda3/lib/python3.7/site-packages/ Sometimes (and I really don't know why) the modules inside sites-packages conda env are not available to import automatically without export the PATH.有时(我真的不知道为什么)在没有导出 PATH 的情况下,sites-packages conda env 中的模块无法自动导入。

So, to solve this issue, you have 2 options:因此,要解决此问题,您有两个选择:

  • Installing using pip install folium and import folium (don't need install by conda install), or使用pip install folium和 import folium pip install folium (不需要通过 conda install 安装),或者

  • After conda install , run conda init , close the terminal and open a new one. conda install 后,运行conda init ,关闭终端并打开一个新终端。 So, try to import again.所以,再次尝试导入。

Here are some tips about use a pip in a conda-environment. 这里有一些关于在 conda 环境中使用 pip 的提示。

You can refer to a specific version of python by using the following at the first line of your .py file This is for python 2.7您可以通过在 .py 文件的第一行使用以下内容来引用特定版本的 python This is for python 2.7

#!/usr/bin/env python2.7

This is for python 3这是用于python 3

#!/usr/bin/env python3

As other users already pointed out you need to check if your module is included in your sys path.正如其他用户已经指出的那样,您需要检查您的模块是否包含在您的系统路径中。 Use code:使用代码:

import sys
print(sys.path)

If not you can include this in your sys.path by using the command:如果没有,您可以使用以下命令将其包含在您的 sys.path 中:

sys.path.append('/path/to/the/folder/of/your/module/file')

or place it in default PYTHONPATH itself.或者将它放在默认的 PYTHONPATH 本身中。

Other great answers: https://stackoverflow.com/a/19305076/5381704其他很棒的答案: https : //stackoverflow.com/a/19305076/5381704

The problem is that in the code editor you are using, you are running the default interpreter.问题是在您使用的代码编辑器中,您正在运行默认解释器。 Based on your code editor, change the python interpreter to the conda interpreter and it will work.根据您的代码编辑器,将 python 解释器更改为 conda 解释器,它将起作用。

In my workstation, I was able to solve No module named <module name> error using two different ways.在我的工作站中,我能够使用两种不同的方式解决No module named <module name>错误。

First method, I solved this temporarily by:第一种方法,我暂时解决了这个问题:

(1) Open a Terminal (1) 打开终端
(2) $ conda activate <Conda environment name> (2) $ conda activate <Conda environment name>
(3) $ export PYTHONPATH=/home/<user name>/anaconda3/envs/<Conda environment name>/lib/<Python package version>/site-packages:$PYTHONPATH (3) $ export PYTHONPATH=/home/<user name>/anaconda3/envs/<Conda environment name>/lib/<Python package version>/site-packages:$PYTHONPATH

It is a temporary solution.这是一个临时解决方案。 Whenever you run your virtual environment, you have to do this.每当您运行虚拟环境时,您都必须这样做。

My runtime environment:我的运行时环境:
OS: Unbuntu 18.04操作系统:Unbuntu 18.04
Conda version: 4.8.2康达版本:4.8.2
Conda-build version: 3.18,11 Conda-build 版本:3.18,11
Python version 3.7.6.final.0 Python 版本 3.7.6.final.0

Second method, I removed the第二种方法,我删除了
alias python=/usr/bin/python3.6 line in bashrc file. bashrc文件中的alias python=/usr/bin/python3.6行。
Somehow this line blocks using Python tools installed in Anaconda Virtual Environment if the Python version in the Virtual Environment is different.如果虚拟环境中的 Python 版本不同,这条线会以某种方式阻止使用安装在 Anaconda 虚拟环境中的 Python 工具。

In my case that happened because conda screwed up the environment variables.就我而言,这是因为 conda 搞砸了环境变量。 Instead of using env-specific python and pip , it used the globally installed ones.它没有使用特定于环境的pythonpip ,而是使用全局安装的。

Solution:解决方案:

conda deactivate your-env
conda activate your-env

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM