Why can't Python find the module I installed? - python

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

Related

Not able to run python file from conda environment

I am not able to run the python file after activating the conda environment in the folder where the python file is.
Below is the screenshot of the error that i am getting after running the python file:-
Below is the screenshot of the folder in which the conda environment and all the other files including python ones are:-
Using /bin/python3 is using the system-level Python, not the one in the environment. Instead, use plain "python" and let the PATH resolve the environment's Python binary.
Or, use conda run, which I find is preferable for programmatic execution within a Conda environment. See conda run --help.
conda run -n PB_2870 python "/path/to/file/script.py"

Installed Python 3, now my Anaconda environment is messed up

I apologize in advance for my poor vocabulary - I do not know much about environments, paths, and things of the sort. I am on macOS Catalina.
I created a program using Spyder from Anaconda. My program uses packages like pandas and numpy which are built into Anaconda. I used to be able to run the program in Terminal with the command: $ python3 app.py.
However, I recently downloaded Python 3.9.1 from https://www.python.org/downloads/mac-osx/. Ever since then, I have been unable to run the program in Terminal because of missing package errors like
import numpy as np
ModuleNotFoundError: No module named 'numpy'
I went to my applications folder, right clicked on Python 3.9.1, and clicked Move To Trash. This did not solve my problem. I reinstalled Anaconda (I did not uninstall it - just simply installed again), but that also did not solve my problem. I am able to run the program in Spyder (from Anaconda), but I wish to run it in Terminal again.
I believe the solution is simple, but I am not sure what to do. I have tried searching and reading but I am not familiar with the terminology. I think I just need to reset the environment, but I am not sure.
Anaconda is used for creating closed enviorments, so you don't need to thrash your computer with global packages.
Imagine you have 2 different projects. Project A works only on python 3.2 and Project B works on 3.8.
That's where anaconda comes in.Managing enviorments with conda
conda create -n PROJECTA python=3.2
conda create -n PROJECTB python=3.8
Now activate env you wish to work with. For macOS
source activate PROJECTA
Now you should see (PROJECTA) instead of (base)
Now inside this PROJECTA you can install modules you require like
pip install numpy
and when executing .py file
move to dir with your app.py file and
python3 app.py
this will be opened in conda enviorment you created and activated, using modules you installed in this env.
You can still edit py file and execute it through shell, but it will throw errors if you try to run it from IDE without linking project to respected conda enviorment.
If you are using PyCharm Configure a Conda vir env in PyCharm
You must create separate environments for every projects or it will get messed up.
conda create -n name_of_environment python=3.6
You must not delete the python folder into trash rather uninstall it Python 3.9.1 and remove its path from the directory.
In short if you run python from terminal and it is not supporting your Installed anaconda packages . You should use anaconda prompt instead of CMD.
Because your anaconda is not added to path rather then it picks up the python 3.9 you have installed from Python 3.9.1 from https://www.python.org/downloads/mac-osx/. ( This is the python with separate environment then anaconda so it wont detect anaconda packages ).
When you run the cmd and enter python it runs the python that you downloaded and installed rather then Anaconda
At the time of installing anaconda it gives option to add conda variables to path you can select those at installing stage / or add manually

Installed modules not found by Python

I need to use the autogui module to do something in Python. When I run the script, it says that it can't find the autogui module so I installed it with
pip install autogui.
But when I run the script again it still says me this module doesn't exist.
Method 1:
You're probably having trouble setting up your correct Python Interpreter and working within it,try the following in VSCode.
Ctrl + Shift + p
And enter the following in the field.
python: select interpreter
Select the desired environment and reinstall PyAutoGui
Method 2:
Creating a virtual environment for your project where all your packages will be installed and will be isolated from others, and will have no import errors since it's an environment specifically for the project that you're working on.
I assume you use Windows, so open the command line in your working directory, or open your working directory in VSCode and enter the following in the command-line tool that is provided within VSCode.
The Python installers for Windows include pip. You should be able to access pip using:
py -m pip --version
You can make sure that pip is up-to-date by running the following
py -m pip install --upgrade pip
Installing virtual environment
py -m pip install --user virtualenv
Creating a virtual environment
py -m venv env
The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.
venv will create a virtual Python installation in the env folder.
Finally, to activate the environment run the following command
.\env\Scripts\activate
That will activate your environment.
pip install pyautogui
Make sure to change your interpreter to the one that you just created in the env/bin folder and run your code, or you could just enter the path to the *python file located in the env/bin folder.
Try pyautogui - I had the same problem. Instead of autogui, write `pyautogui. Or, if you are running python3 or higher then try:
pip3 install pyautogui.

Cannot access numpy in cmd

I have Anaconda3 installed with Python3.7.1 in C:\Anaconda3. I have set following as my environment variables: C:\Anaconda3; C:\Anaconda3\Scripts; C:\Anaconda3\python.exe; C:\Anaconda3\Lib\site-packages.
On Windows10, cmd recognises python and runs simple programs but throws ImportError when I try to access numpy. Numpy shows as installed in Anaconda GUI and I can access it through conda Command Prompt.
I need help to run my programs from cmd.
Edit: pip install recognises numpy as -
Requirement already satisfied: numpy in c:\anaconda3\lib\site-packages (1.15.4)`
In Anaconda 5.3.0 the numpy version accesses libraries that are on the system PATH set by conda when you use start the Anaconda Prompt or when you run:
conda activate base
from the CMD prompt. This sets a handleful of environmental variables, but the important one is what conda adds to PATH. There are a handleful of ways around this. One is you can simply use the Anaconda Prompt to run your script, or you can run the above command before running your Python code.
Alternatively, you can also add a batch file to your Anaconda installation that activates the activates the environment and then passes any additional arguments to python.
Here is the content of the batch file
# file name:
# C:\Anaconda3\python.bat
call C:\Anaconda3\Scripts\activate.bat base
C:\Anaconda3\python.exe %*
Now you can call you script by activating the base environment first and then firing the script via:
python.bat myscript.py

Package installed by Conda, Python cannot find it

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.

Categories

Resources