Package installed by Conda, Python cannot find it
Asked Answered
F

6

29

I try to install Theano by Anaconda. It works, but when I enter the python -i, import theano shows No module named 'theano'. Do I need to switch another interpreter of Python, how? Also, for the packages installed by conda, if I don't double install them, can I find in Python? How is Python related to Python by Anaconda? Thanks!!!

Flatten answered 1/10, 2016 at 22:10 Comment(5)
You said "It works" - what do you mean by that? The installation works, or using theano works? What version of Python do you get by running python on the command line - what is the full text of the header that prints out? Finally, what operating system are you using?Lenticularis
The installation is successful, but I couldn't find theano package in python. The python I use is 3.5, 32-bit, and Anaconda is the latest, 32-bit. The operating system is Windows 10. If I type {pip install theano}, it appears {Requirement already satisfied (use --upgrade to upgrade): theano in d:\anaconda3\lib\site-packages}. Thank you! @LenticularisFlatten
Please run the following in python: import sys; print(sys.path) and see if D:\Anaconda3\lib\site-packages is included.Lenticularis
After I added that to PYTHONPATH, the import theano works! Though it seems that there are other problem in the installation, Theano do not support Python 3.5 on Windows. and Mingw and Python3.5* were found to be in conflict. May I ask how to install python2.7 and python3.5 both and refer to them? Thanks a lot! @LenticularisFlatten
conda update --all fixed the issue for me on a different package but same problem.Razid
C
17

I had have a similar issue, trying to install folium. If you are using the Anaconda:

When you install using conda install -c conda-forge folium, the package will be placed in:

./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:

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

Python use first the sites-packages as the target directory of manually built python packages. 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.

In this case you have two places: /anaconda3/lib/python3.7/site-packages/ and /anaconda3/envs/[name env]/lib/python3.7/site-packages/.

First the modules will be available as default in /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.

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

  • After conda install , run conda init, close the terminal and open a new one. So, try to import again.

Here are some tips about use a pip in a conda-environment.

Czarist answered 28/3, 2019 at 18:42 Comment(2)
If any one using miniconda3 got here like me, you will need to add /miniconda3/pkgs to path.Downstairs
Also to anyone using miniconda3, make sure your python points to miniconda3/envs/<your_env>/bin/pythonLanglois
V
3

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

#!/usr/bin/env python2.7

This is for 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.append('/path/to/the/folder/of/your/module/file')

or place it in default PYTHONPATH itself.

Other great answers: https://mcmap.net/q/15280/-should-i-put-shebang-in-python-scripts-and-what-form-should-it-take

Vacua answered 2/10, 2016 at 2:17 Comment(2)
Yep. And there is also Python in Anaconda, when I typed Python, which one am I running? Since I added Anaconda in PYTHONPATH, are they sharing the package with each other? What about the system requirement of different packages, e.g. one for Python2.7, one for Python3.5? I am just so confused with different versions and their application. Thanks!Flatten
when you type PYTHON you use python 2.7Vacua
N
1

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.

Nady answered 10/2, 2021 at 15:52 Comment(0)
R
1

In my case that happened because conda screwed up the environment variables. Instead of using env-specific python and pip, it used the globally installed ones.

Solution:

conda deactivate your-env
conda activate your-env
Recalcitrate answered 29/10, 2021 at 9:8 Comment(1)
This didn't solve my issue, which is the same as OPMcclimans
A
0

In my workstation, I was able to solve No module named <module name> error using two different ways.

First method, I solved this temporarily by:

(1) Open a Terminal
(2) $ conda activate <Conda environment name>
(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
    Conda version: 4.8.2
    Conda-build version: 3.18,11
    Python version 3.7.6.final.0

Second method, I removed the
alias python=/usr/bin/python3.6 line in bashrc file.
Somehow this line blocks using Python tools installed in Anaconda Virtual Environment if the Python version in the Virtual Environment is different.

Apologize answered 1/7, 2021 at 22:7 Comment(0)
G
0

Actually for Spyder: the iterator is reset to the default, and you just need to re-select the in Tool -> Preferences -> python interpreter

Gemoets answered 30/5, 2023 at 14:46 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Loxodrome

© 2022 - 2024 — McMap. All rights reserved.