Package installed by Conda, Python cannot find it - python

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!!!

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.

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

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

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.

Related

Why can't Python find the module I installed?

Presently I'm using Python on a Windows system. I installed Python 3.10 from Anaconda and also the Pycharm IDE. I have ensured that Python is in the correct path in the environment variable. I have also replicated this problem using two different versions of Python, 3.10 and 3.9.
Very simply, in PyCharm, I open a terminal and type
conda install -c numpy numpy.
Then, I write a new "main.py" script. I have one line: "import numpy". I receive the error:
Traceback (most recent call last):
File "C:\Users\---\PycharmProjects\pythonProject3\main.py", line 17, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
What am I doing wrong?
Going on advice from a friend, I created a new PyCharm project sitting not in my user directory but on the C: drive, and got the same error. Finally, when trying to re-install the package using either using either pip or conda, I get this message:
# All requested packages already installed.
You have 2 versions of Python:
Default Python (used everytime you open your command prompt and type python or python3)
Anaconda is installing packages in a virtual environment, using it's own Python (it is located in a different path)
You can see the path of your installed python using python -c "import os, sys; print(os.path.dirname(sys.executable))"
You have 2 Options:
Configure the PyCharm in order to use the anaconda Python. https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#view_list
Open a command prompt in the project's folder (you can do it easily using PyCharm). Type conda env list. This will show you all available anaconda virtual environments. Choose 1 of them and type conda activate <env_name>, where <env_name>=the name of the environment. Then, run your program using python <name_of_your_program>
You can see the paths where the anaconda environments and packages are installed using conda info
There main reason for this is
You are running your main.py in different environment rather than where you installed numpy.
If you trying to run it via cmd use this method
Check which environment you are in right now. refer this and
this. But the most easiest way to do this is use where command
in windows cmd. C:\> where python or C:\> where python3. You will
get the path of activated interpreter.
list conda envs - conda env list
activate conda env - conda activate <env name>
then run this command. pip freeze . and check is there numpy in
the list. If not you have to find and activate the environment where
you have installed numpy.
If you want to run it in pycharm
Refer this on how to change pycharm interpreter.
https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html
Many things can cause this, usually its one of these
You may have to restart your terminal, or IDE if running in there, after installing a package to "refresh" the environmental path
The package is not in the environmental path

No module named pandas in command prompt

I'm getting the error in the title when running a .py script. I have pandas installed (version 0.24.2) as you can see in the screenshot. The Python version is 3.7.3. When I run the command import pandas as pd in a Jupyter notebook it works. However, when I have the same command in a .py file ("MyFile.py") and try to run that file using Anaconda command prompt I get this error. Same happens when using the Windows command prompt. Can someone please advise on how to solve this?
The 2nd screenshot above shows that I can't find a folder for pandas in site-packages but anaconda seems to think it's installed.
have you tried python3.7 MyFile.py?
If not, I would try it. Then if it doesn't work I'd create a new environment with python 3.7, and then install pandas and run your code:
conda create -n test_env python=3.7
conda activate test_env
pip install pandas
python MyFile.py
Probably you have installed Python and afterwards Anaconda.
AnaConda has its own python exe and Libs that are automatically installed. They're probably located in (Windows path): C:\ProgramData\AnacondaX\
So, if you go to C:\ProgramData\AnacondaX\Lib\site-packages\ you will see a folder "pandas". It means that Pandas lib is installed for Anaconda exclusively.
On the other hand, you have installed Python in c:\Python27. I'd advise you firstly add to %PATH% environment variable to get access to Python from any folder you're in and after please check the libs you have installed in your Python (not Anaconda) with (pandas is not listed there):
c:>pip freeze
Next step is to install pandas for your Python installation. Just do it:
c:>pip install pandas
Anaconda uses its own python installation, which by default, and as per this documentation, should be under C:\Users\<your-username>\Anaconda3\.
By installing pandas via Anaconda, and then specifically running the script with a different python installation, you're running a script using a library within environment that does not have that library installed.
You can try installing pandas to this separate python installation (I saw your pip install didn't work, you could again specify this python installation and use pip as a module):
'C:\python27\python.exe' -m pip install pandas
Or run the script with the Anaconda python installation by specifying that python installation:
{path_to_anaconda_python} MyFile.py
Where path_to_anaconda_python is path to Anaconda python installation (by default, within C:\Users\<your-username>\Anaconda3\)

How to pip install python packages when anaconda is installed?

I installed anaconda and now I can't use pip to install packages on my version 3.6 of python, instead it installs them on anaconda. If I type pip -V I get:
pip 9.0.1 from /anaconda/lib/python3.6/site-packages (python 3.6)
so how do I make it so that it shows:
/Library/Frameworks/Python.framework/Versions/3.6/
instead?.
Or am I missing something?
Well, anaconda comes with its own python directory, which replaces your default python and thus renders its pip useless.
You need to edit .bashrc (or .zshrc in case you are using zsh). There you need to change the path to your original python directory. First of all delete anaconda related export PATH and put in:
export PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:$PATH"
If you are using Debian based system. You can also try update-alternatives. This is very powerful (yet easy to use) tool, which allows you to change current default python version without manually updating .bashrc.
How to use update-alternatives can be found here: https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux#h2-change-python-version-system-wide
I had to work this out myself awhile back:
On Windows 10, I entered command prompt (iecmd) and used:
python -m pip install --target=C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\
modulename
I assume if you can find the folder your python 3.6 then you'll be able to use:
python -m pop install --target=whereever\your\python\is themoduleyouwant
Note that the space between the path and the module name is necessary.

How to configure pip to install packages for python 2.x and python 3.x separately

I am running Windows x64 bit.
I downloaded the Pyro4 package via pip install Pyro4. It downloaded the packages successfully and they are all present in my "C:\Python34\Scripts" folder as I've kept Python3.4 as default.
Now when I went to the that "C:\Python27\Scripts" folder, the Pyro4 package is not to be found. This is as expected, but I would like to work on both Python 2.7 and 3.4 as Pyro4 is compatible in both.
How do I change my pip command to download the package to Python 2.7's installation scripts directory?
First make a new environment variable:
Go to your system properties
Under Advanced tab click Environment Variables...
Under System variables section click New...
Variable name: (whatever you can remember for example p27s)
Variable value: your python 2.7 scripts folder ("C:\Python27\Scripts\")
From now on whenever you want to install a package for python 2.7 you can do it this way: %your_variable_name%pip install package_name
For example: C:>%p27s%pip install Pyro4
This way you can install any package for python 2.7 and use default pip for python 3.4
You will need to go to your environment variables in the control panel and change the path from C:\Python34\Scripts to C:\Python27\Scripts. After that change, when you type 'python' in the command prompt it will be using Python 2.7. Next, install pip like you initially did.
Not exactly what you're asking, but you could check out Anaconda which allows you switch python environments really easily.
https://www.continuum.io/downloads
Edit
Just to clarify
if you were to use this method, you can have named environments say python27 and python34
then you can just:
activate python27
pip install module
activate python34
pip install module
and it will install them to both environments.

Python ImportError while module is installed [Ubuntu]

I'd like to make a switch from Windows to Linux (Ubuntu) writing my python programs but I just can't get things to work. Here's the problem: I can see that there are quite the number of modules pre-installed (like numpy, pandas, matplotlib, etc.) in Ubuntu. They sit nicely in the /host/Python27/Lib/site-packages directory. But when I write a test python script and try to execute it, it gives me an ImportError whenever I try to import a module (for instance import numpy as np gives me ImportError: No module named numpy). When I type which python in the commandline I get the /usr/bin/python path. I think I might need to change things related to the python path, but I don't know how to do that.
You can use the following command in your terminal to see what folders are in your PYTHONPATH.
python -c "import sys, pprint; pprint.pprint(sys.path)"
I'm guessing /host/Python27/Lib/site-packages wont be in there (it doesn't sound like a normal python path. How did you install these packages?).
If you want to add folders to your PYTHONPATH then use the following:
export PYTHONPATH=$PYTHONPATH:/host/Python27/Lib/site-packages
Personally here are some recommendations for developing with Python:
Use virtualenv. It is a very powerful tool that creates sandboxed python environments so you can install modules and keep them separate from the main interpreter.
Use pip - When you've created a virtualenv, and activated it you can use pip install to install packages for you. e.g. pip install numpy will install numpy into your virtual environment and will be accessible from only this virtualenv. This means you can also install different versions for testing etc. Very powerful. I would recommend using pip to install your python packages over using ubuntu apt-get install as you are more likely to get the newer versions of modules (apt-get relies on someone packaging the latest versions of your python libraries and may not be available for as many libraries as pip).
When writing python scripts that you will make executable (chmod +x my_python_script.py) make sure you put #!/usr/bin/env python at the top as this will pick up the python interpreter in your virtual environment. If you don't (and put #!/usr/bin/python) then running ./my_python_script.py will always use the system python interpreter.
/host/Python27/Lib/site-packages is not a default python directory on linux installations as far as I am aware.
The normal python installation (and python packages) should be found under /usr/lib or /usr/lib64 depending on your processor architecture.
If you want to check where python is searching in addition to these directories you can use a terminal with the following command:
echo $PYTHONPATH
If the /host/Python27/Lib/site-packages path is not listed, attempt to use the following command and try it again:
export PYTHONPATH=$PYTHONPATH:host/Python27/Lib/site-packages
If this should work and you do not want to write this in a terminal every time you want to use these packages, simply put it into a file called .bashrc in your home folder (normally /home/<username>).
When installing other python libraries, specify the pip version you want to install it to, if it's python2 you use, then enter this syntax:
pip2 install <package>
For python3
pip3 install <package>

Categories

Resources