Jupyter ModuleNotFoundError - python

I have python 3.7 and anaconda3 installed in Win10, and conda 4.7.12, conda is on my PATH. Had installed jupyter using pip (pip 19.3.1). I can see all packages in the the anaconda directory. Don't have any old python2 or anaconda2 nor any virtual env.
I have jupyter notebook - but for just about every package jupyter is complaining - ModuleNotFoundError. I did pip install for pandas, numpy etc in jupyter - it worked. But juypter complains about the next package, on and on. Do I have to do a pip install inside jupyter for every package or there is better/easier way.
(I know its never easy)

You should first check if you are using the same virtual for both running the notebook and installing packages. If so, then yes, if the modules are not installed you'd have to do it manually or create a requirements.txt file with all the modules you need and execute the following (within the same virtual environment):
pip install -r requirements.txt
See https://pip.pypa.io/en/stable/user_guide/#requirements-files for more information.
I'd also recommend installing the notebook extensions as per https://docs.anaconda.com/anaconda/user-guide/tasks/use-jupyter-notebook-extensions/ to deal with your virtual environments and packages.

Related

Installed python package to conda virtual environment, ModuleNotFoundError in Jupyter Notebook

Installed python package from the Anaconda Prompt in the virtual environment.
import transformers works from the Anaconda Prompt. However, ModuleNotFoundError when in Jupyter Notebook even though kernel is set to the virtual environment where the package is installed.
It looks like the issue is similar to
Jupyter notebook can't load installed package in conda environment, but I can't get it to work on Windows.
Here is an example.
Package is installed in the virtual environment.
Importing from the anaconda prompt works.
Importing from Jupyter Notebook causes the error.
Open the Jupyter notebook from command line. Activate your conda environment first.
Open command line. Activate your conda environment.
Type activate <your env name>
Type jupyter notebook <path to your project>. To open in current directory jupyter notebook . .
The package was initially installed using conda install -c conda-forge transformers.
Instead, I installed the packaged using pip install transformers. Importing the package from Jupyter Notebook on the virtual environment's kernel worked fine afterwards.
When I did where conda from the virtual environment, conda install doesn't seem to be installing to the venv. Whereas where pip showed that the venv's pip is being used.

How to make Jupyter Notebook import packages in Python3.5

I have several packages being imported perfectly in my Python 3.5. But not in my Jupyter Notebook... When i try to Import those packages in Jupyter i get and error of module not found.
Is there a way to make Jupyter load my Python 3.5 as a kernel... or something similar. I'm working in a virtual environment. Already tried to reinstall the packages again in my virtual env But no success.
Try to install the packages inside a jupyter notebook cell like this:
!pip install package
So you are sure that the packages are installed in jupyter's environment
If you install the ipython kernel form inside the virtualenv, you can guarantee that the packages are imported to the jupyter if they are imported to this env. Also, if you use this approach, you do not need to activate the virtualenv every time you run the jupyter, because jupyter does it automatically.
$ python -m venv projectname
$ source projectname/bin/activate
(venv) $ pip install ipykernel
(venv) $ ipython kernel install --user --name=projectname
(venv) $ pip install {package needed to install}
Source: Using jupyter notebooks with a virtual environment

Trying to install a pip package in Anaconda

I'm trying to follow this tutorial:
https://learn.microsoft.com/en-us/azure/machine-learning/service/tutorial-data-prep
As part of this I'm trying to do a pip install of azureml as it's not available on conda. However doing a pip install will by default install it to my default python install, and not my conda install.
So I tried following the steps here:
https://conda.io/docs/user-guide/tasks/manage-environments.html#using-pip-in-an-environment
However after following these steps I then launch Jupyter notebook after activating myenv, navigate to the notebook, and try and run:
import azureml.dataprep as dprep
But get the error: ModuleNotFoundError: No module named 'azureml'
Also - I cannot tell if myenv is active in the notebook. The kernel simply says python3.
Be careful, when using pip in anaconda, it is possible that you are mixing pip and pip3.
Run which pip3 to be sure you are using the version that correspond to the virtual environment.
If you are using python3 in the environment, then pip will typically be the correct version to use. Do not use pip3 in that case.
This problem has been documented elsewhere on the web. The problem is that Jupyter notebooks itself only launches in the root environment by default. The simplest solution to getting it to launch for your env (e.g. myenv) is to install Jupyter within your env first. So from the Anaconda command prompt:
activate myenv
pip install jupyter
jupyter
Ps. Use source activate myenv for non-windows machines

How to link virutualenvwrapper with Jupyter notebook

I have a venv/virtualenvwrapper set up in a directory. After I start working on the venv, i then create a Jupyter notebook inside of the directory, which brings me to Jupyter's localhost browser. I then attempt to import a library, e.g. pandas, which raises the ModuleNotFoundError. I would think that if the venv is being worked on, Jupyter would link to it.
Also, I can import global libraries from pip3, just not the local ones in a venv. Is there a way to use the virtualenvwrapper library with Jupyter?
I found out here I had to install ipykernel after activating the venv, then create a projectname:
​(venv) $ pip install ipykernel
(venv) $ ipython kernel install --user --name=projectname
​
After that, I went back to the Jupyter browser and change the kernel from python 3 to the projectname which is only viewable after executing the second command line above.
This works for both virtualenv and virutalenvwrapper.
The only downfall is installing ipykernel adds a lot of extra libraries to your venv pip3, but I suppose you could just install ipykernel in your global pip3 to keep from installing it in your venv.

How do I automatically run on an environment with Spyder?

Sorry if I am unclear/this is a lame question, still very new to setting up my computer.
So I installed Python using Anaconda for work, and at work there are specific packages that are used internally. I used Anaconda Prompt to install these packages by creating an environment and installing internally used packages. I then check to see if the package is there.
In the Anaconda Prompt...
conda create --name environment_name python = 3.4
activate environment_name
pip install <internal package> --upgrade
pip list
However, when I try to import a package on Spyder, it does not recognize the package. Is there a way for Spyder to run code on an environment that I specify? I would like the environment to run automatically on any file that I run using Spyder, being able to pull the packages that I installed on Anaconda Prompt

Categories

Resources