I have downloaded anaconda and always used Jupiter Notebook, and Spyder with no problem. Now I need to run some code on my computer's python shell (I don't know if this is the appropriate name, I am quite new to coding) but when trying to import numpy I get:
ModuleNotFoundError: No module named 'numpy'
I have tried everything whit pip but is not resolving the problem, it actually shows a syntax error. Should I try to uninstall and install everything? I don't really know how to move, thanks a lot in advance.
When you start a new terminal instance the base conda environment is not activated, so when you do python, the interpreter that is called is not Anaconda's Python, it is most likely the system-wide Python installation.
So to access Anaconda's Python from the terminal:
conda activate to activate the base conda environment
which python should show the path to the Anaconda interpreter
python
Interactive shell:
import numpy
Anaconda is a virtual environment for python. It does not install packages globaly. I strongly recomend reading about VENV (virtual enviroment).
You can install does packages globally, by pip:
pip install numpy
or for python3:
pip3 install numpy
Related
From the start of using pycharm i am facing problems with working with libraries
I tried reinstalling python, pip, pycharm; adding and readding to path. I also tried using pipenv instead of virtenv.. and it worked once, but.. now again-
i use $pip install numpy |as example| in cmd window it says to be successfully installed. Go to pycharm, type 'import numpy'.. and nothing happens. I know i can download manually: go to settings and so on.. but it would be much beter if pip installed with cmd would be instantly displayed in Pycharm. Anybody, please, help.
Check if you have activated the virtual environment in which you have installed the packages. For instance you may have installed the package on Global python version and running your program on a virtual environment which will not work. So maybe try activating your virtual environment before installing the Packages.
Step 1:-
activate {name_of_pipenv}
pip install numpy
I am working in a jupyter notebook, and used pip to install ANTsPy:
pip install antspyx
However, using the function from_numpy throws an error:
import ants
Im2Use=Im[0,:,:,:]
fixed, moving, mytx=reg(Im2Use, t_rz)
fwdtransforms=mytx['fwdtransforms']
fixed_ants= ants.from_numpy(Im2Use.astype(float))
moving_ants= ants.from_numpy(t_rz.astype(float))
mywarpedimage=ants.apply_transforms( fixed=fixed_ants, moving=moving_ants,transformlist=fwdtransforms)
AttributeError: module 'ants' has no attribute 'from_numpy'
How can I solve this? I've also tried importing ants using import ants.from_numpy, but that did not work.
Can you please specify your IDE+ if you took care of activating the virtualenv?
Anyways here are the steps to make sure that the python module is propoerly installed in your virtual environment :
python3 -m venv env_name : this will create a virtual env in your current path
source env/bin/activate : this will activate your virtual env (if on windows just cd into activate within your env name .\env\Scripts\activate
If your using vscode hit ctrl+shift+p then select interpreter then go to your created env
now we have our virtual environment just hit pip install antspyx then check with pip list and see in your terminal if the package is propoerly installed
If you still have the error restart your IDE
have you tried:
from ants import from_numpy
The issue in this case was scipy versioning. I downgraded from version 1.7.3 to version 1.2.0, which then solved the problem. Why? It's because ants library expects a function called "factorial" in scipy.misc, but version 1.7.3 moved the location of factorial.
I am currently trying to work basic python - jupyter projects.
I am stuck on following error during matplotlib:
screenshot on jupyter-error
ModuleNotFoundError: No module named 'matplotlib'
I tried to update, reinstall matplotlib aswell in conda and in pip but it still not working.
happy over every constructive feedback
In a Notebook's cell type and execute the code:
import sys
!{sys.executable} -m pip install --user matplotlib
and reload the kernel
(src: http://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/ )
open terminal and change the directory to Scripts folder where python installed. Then type the following command and hit enter
pip install matplotlib
Hope this will solve the issue.
I was facing the exact issue. It turns out that it was using the system Python version despite me having activated my virtual environment.
This is what eventually worked.
If you are using a virtual environment which has a name say myvenv, first activate it using command:
source activate myvenv
Then install module ipykernel using the command:
pip install ipykernel
Finally run (change myvenv in code below to the name of your environment):
ipykernel install --user --name myvenv --display-name "Python (myvenv)"
Now restart the notebook and it should pick up the Python version on your virtual environment.
While #Frederic's top-voted solution is based on JakeVDP's blog post from 2017, it completely neglects the %pip magic command mentioned in the blog post. Since 2017, that has landed in mainline IPython and the easiest way to access the correct pip instance connected to your current IPython kernel and environment from within a Jupyter notebook is to do
%pip install matplotlib
Take a look at the list of currently available magic commands at IPython's docs.
generally speaking you should try to work within python virtual environments. and once you do that, you then need to tell JupyterLab about it. for example:
# create a virtual environment
# use the exact python you want to work with in this step
python3.9 -m venv myvenv
# 'activate' (or 'enter') it
source myvenv/bin/activate
# install the exact stuff you want to use in that environment
pip install matplotlib
# now tell JupyterLabs about the environment
python -m ipykernel install --user --name="myenv" --display-name="My project (myenv)"
# start it up
jupyter notebook mynotebook
# if you now look under 'Kernel->Change kernel', your 'myenv' should be there
# select it (restart kernel etc if needed) and you should be good
The issue with me was that jupyter was taking python3 for me, you can always check the version of python jupyter is running on by looking on the top right corner (attached screenshot).
When I was doing pip install it was installing the dependencies for python 2.7 which is installed on mac by default.
It got solved by doing:
> pip3 install matplotlib
Having the same issue, installing matplotlib before to create the virtualenv solved it for me. Then I created the virtual environment and installed matplotlib on it before to start jupyter notebook.
in jupter notebook type
print(sys.executable)
this gave me the following
/Users/myusername/opt/anaconda3/bin/python
open terminal, go into the folder
/Users/myusername/opt/anaconda3/bin/
type the following:
python3 -m pip install matplotlib
restart jupyter notebook (mine is vs code mac ox)
If module installed an you are still getting this error, you might need to run specific jupyter:
python -m jupyter notebook
and this is also works
sudo jupyter notebook --allow-root
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.
I'm trying to import cv2 on Jupyter notebook but I get this error:
ImportError: No module named cv2
I am frustrated because I'm working on this simple issue for hours now. it works on Pycharm but not on Jupiter notebook. I've already installed cv2 into Python2.7's site packages, configured Jupyter's kernel to python2, browsed the documentation but I still don't get what I am missing ?
(I'm using windows 10 and working with microsoft cognitives api, that's why I need to import this package.)
here is the code:
<ipython-input-1-9dee6ed62d2d> in <module>()
----> 1 import cv2
2 cv2.__version__
What should I do in order to make this work ?
Is your python path looking in the right place? Check where python is looking for the module. Within the notebook try:
import os
os.sys.path
Is the cv2 module located in any of those directories? If not your path is looking in the wrong place. If it is overlooking the install location, append it to your python path. You can follow the instructions here.
I didn't have the openCV installation in my Python3 kernel, so I installed it by activating the specific environment and running this in the command prompt:
pip install opencv-python
How to find and activate my environment?
To list all of Your conda environments, run this command:
conda info --envs
You will get something like this:
ipykernel_py2 D:\Anaconda\envs\ipykernel_py2
root D:\Anaconda
After that, activate the environment that is complaining for the missing cv2 and run the pip install opencv-python command.
How to activate an environment?
Just run the command:
activate env_name
where env_name is the wanted environment (for example, You could type activate ipykernel_py2 if You wanted to access the first of the two environments listed above).
Note: If You are on Linux, You need to type source activate env_name.
Go to your notebook, in menu section
kernel -> Change kernel -> Python<desired version>
Now in the notebook run following command to install opencv2 in the selected environment kernel
python2:
!pip install opencv-python
python3:
!pip3 install opencv-python
Binmosa's explanation is great and to the point. As an alternative (easier, but I'm pretty sure it's just a band-aid fix), if you write:
import sys
!{sys.executable} -m pip install opencv-python
directly into your notebook, you'll be able to actually install the module in the notebook itself.
The longer explanation is interesting and informative, though. Link: https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/
To make this clear for those who are having the same issue:
By default: Anaconda (jupyter notebook) has its own version of Python & packages once it has been installed on your PC.
If you have Python x.x installed on your PC, and you installed OpenCV or -whatever packages- using the package manager of this python version, it does NOT mean your jupyter notebook will get access to these python packages you installed earlier. They are not living in the same folder.
To illustrate this, open your windows CMD and write :
python
then write:
import os
os.path
you will get the path of your python. in my case (C:\Python35)
Now open the Anaconda Prompt and write the same commands again:
you will get the anaconda's python path. In my case (C:\Users\MY_NAME\Anaconda3).
As you can see, there are two different paths of python, so make sure that your first step in diagnosing such error (No module named x) is to ask yourself whether you installed the package in the right place or not!
N.B: within Anaconda itself you can create environments, each environment may have different packages installed in it, so you also have to make sure you're in the right environment and it is the active one.
It is because of opencv library.
Try running this command in anaconda prompt:
conda install -c conda-forge opencv
You can simply open Jupyter Notebook and in any of the cell, just write:
pip install opencv-python
It will automatically install the file
Note : Keep turn ON your Internet connection
Then in next cell :
import cv2
It will work.
I added \envs\myenv\Library\bin also in the path variable and it got solved.
You will need to install ipykernel for the jupyter notebook. Follow the following steps:
python -m virtualenv env
source env/bin/acitivate
pip install opencv-contrib-python
pip install ipykernel --upgrade
python -m ipykernel install --user
jupyter notebook
You can simply try this in your jupyter notebook cell `%pip install opencv-python`
no matter which python version you're using. you may need to restart kernel to use updated package
I had this issue in my Jupyter Notebook after I had "installed" the opencv package, using Anaconda Navigator, on my base (root) environment.
However, after "installing" the package and its dependencies, Anaconda Navigator showed a reminder popup to update to the next Anaconda Navigator version. I ignored this at first, but couldn't use the opencv package in my Jupyter Notebook.
After I did update Anaconda Navigator to the newer version, the opencv package install worked fine.
pip install opencv-python
This solved the error for me in MacOS.
I had similar problem. None of the above solution worked for me. I did below in my notebook and that solved the issue
!pip install opencv-python
!pip install opencv-python-headless
I hope you have already activated the environment you know OpenCV is installed in but is not running/import error in jupyter notebook.
If not then run the below command and activate your environment before running the jupyter notebook.
conda activate /Users/prajendr/anaconda3/envs/cvpy39
Then, check all the anaconda environments on your machine using the below command on the jupyter notebook.
!conda info --envs
The output would be similar -
Try to install OpenCV in the environment again.
You know that you have OpenCV installed in this anaconda environment - cvpy39 and the path is "/Users/prajendr/anaconda3/envs/cvpy39/lib/python3.9/site-packages"
Then type the below commands to see if the OpenCV path was imported in the notebook or not?
import os
os.sys.path
you see the OpenCV path is not in this list so you need to manually import it.
Then in a cell type the below set of code. Make sure to change the python path of the environment to yours.
import sys
path_to_module = "/User/prajendr/anaconda3/envs/cvpy39/lib/python3.9/site-packages/"
sys.path.append(path_to_module)
import cv2
You will now be able to import OpenCV to your jupyter notebook.
One of possibility is that you could have written import cv2 and its utilisation in separate cells of jupyter notebook.If this is the case then first run the cell having import cv2 part and then run the cell utilising the cv2 library.