Jupyter notebook doesn't see the environment's packages - python

I want to follow along a course about deeplearning. This course has a github repo, where all of the codes are available. We need to use anaconda, create an environment based on the yaml file, and run a jupyter notebook within that environment.
There's even a test notebook to check whether the installation and environment creating process was successful. I have created the environment as the description said, the installation of the packages seemed to be good.
For the first few command, I get the required outputs, but the notebook doesn't seem to start from the correct environment, it gives me
C:\Users\user\Anaconda3\Lib\site-packages\jupyter.py
instead of
C:\Users\paperspace\Anaconda3\envs\ztdl\lib\site-packages\jupyter.py
And it can't find the packages that I have just installed before. For example, when it has to include sklearn it gives me:
No module named 'sklearn.__check_build._check_build'
Installing them from command line with pip/conda solves this.
I have tried to install manually ipykernel, nb_conda, jupyter as other posts say. I ran jupyter notebook after I activated the conda environment from the anaconda prompt. But none of these helped me. As I open the kernel menu, I can see that the proper ztdl anaconda environment is the active one.
I also removed anaconda from the %PATH%, and added it again afterwards with c:\Users\user\Anaconda3\ .
I have no idea what should I do to run the notebook within this given anaconda environment, so I can access the installed packages. Thanks in advance, any help is appreciated.

Related

Using command line in Jupyter Notebook not executing commands in virtual environment

I need to use the command line in Jupyter Notebook in a virtual environment, but the commands don't execute in the environment itself. I'm using Anaconda Navigator to open Jupyter Notebook. For background, I use a Mac.
Whenever I do something like ! pip install xyz, it installs the package in the Anaconda directory for libraries (see the image). I made sure that the kernel is in the environment, so that's not the problem.
What's interesting is that if I try to import a package, I can only import packages from the environment itself, not the Anaconda libraries. In other words, installing packages doesn't occur in the environment, but importing packages does.
Image that shows the pip install
This wouldn't matter that much because I could download packages in the Terminal itself, but I also need to do things like in the image below in my virtual environment, which is not possible to do in the Terminal (or is it?).
Example of what I need to do
Any ideas on how to fix this?
EDIT: That error is fixed. However, I'm getting an error if I try to use a Python command: image of error. The first line just defines the file path for the script (the last item in the list is a .py file). I tried doing "%%python" followed by the command, but it doesn't work. Any ideas?

Difference between Jupyter and Terminal in the same Kernel

I'm trying to import tensorflow as ts in my script. While everything is fine in a Notebook, when I try to recreate the same script in a .py file the import returns the following, popular message:
ModuleNotFoundError: No module named 'tensorflow'
Note that both Jupyter and terminal are using the same virtual environment. In Notebook it's picked as a Kernel and in terminal it's activated with conda.
I was convinced the whole Kernel/env idea was about preventing exactly that kind of situations. Although I'm new to python so maybe I'm missing something basic.
Also, this might have something to do with that I'm using an M1 and macos, so I have the tensorflow-macos installed. But please consider there might be other reasons.
Make sure your pyenv installation doesn't interfere with the conda environments. Pyenv can overrule which python installation is used even if the conda environment has picked another. In my case it was the reason. Jupyter Notebook wasn't affected by pyenv. There are two solutions:
Remove pyenv
Make sure it is clear to you how both pyenv and conda are affecting your setup and correct it. Some considerations and suggestions are mentioned in this question: Installing anaconda with pyenv, unable to configure virtual environment

Resolve package loading issues for Anaconda Navigator

I have created a new virtual environment "pytorchplay" within Anaconda Navigator to play around with the Pytorch library using Python 3.8. To this environment I loaded Pytorch and Jupyter (and all dependent sources). If I launch an Anaconda terminal prompt from that newly created environment within Navigator and type "conda list," Pytorch appears in the list. If I launch an iPython terminal prompt FROM THE SAME newly created environment and type "pip list," Pytorch does not appear. If I initiate a jupyter notebook FROM THE SAME environment, and type "pip list," Pytorch also does not appear. In this last case, I do have a local kernel instantiated with the correct virtual environment. So, three versions of ostensibly the same instance, but with differing libraries/packages. How is this possible? (Apologies if some of the language is imprecise, I'm learning...)
I have checked any number of similar posts and questions, but cannot seem to locate the correct fix. Should it be helpful, I have checked the paths. These appear below. I would appreciate any assistance that can be provided. Thanks!
From the Anaconda shell:
(pytorchplay) C:\Users\rk264116>echo %PATH%
C:\Users\rk264116\Miniconda3\envs\pytorchplay;
C:\Users\rk264116\Miniconda3\envs\pytorchplay\Library\mingw-w64\bin;
C:\Users\rk264116\Miniconda3\envs\pytorchplay\Library\usr\bin;
C:\Users\rk264116\Miniconda3\envs\pytorchplay\Library\bin;
C:\Users\rk264116\Miniconda3\envs\pytorchplay\Scripts;
C:\Users\rk264116\Miniconda3\envs\pytorchplay\bin;C:\Users\rk264116\Miniconda3\condabin;
C:\Users\rk264116\Miniconda3;C:\Users\rk264116\Miniconda3\Library\mingw-w64\bin;
C:\Users\rk264116\Miniconda3\Library\usr\bin;
C:\Users\rk264116\Miniconda3\Library\bin;
C:\Users\rk264116\Miniconda3\Scripts;
And from the iPython shell:
In [5]: sys.path
Out[5]:
['C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\Scripts',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\python38.zip',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\DLLs',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\lib',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay',
'',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\lib\\site-packages',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\lib\\site-packages\\win32',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\lib\\site-packages\\win32\\lib',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\lib\\site-packages\\Pythonwin',
'C:\\Users\\rk264116\\Miniconda3\\envs\\pytorchplay\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\rk264116\\.ipython']
The explanation is simply that there is a name discrepancy between the conda package (and the libraries name) PyTorch and the name of the python module that you are importing torch.
The package that conda installs and therefore knows about is called pytorch, hence you see that from anaconda navigator or when running conda list.
pip on the other hand is oblivious of what conda does. Since it is only able to manage python modules, it sees the torch package present in your environment.

Can not import library installed in Anaconda 2 while operating with different environments

I have Anaconda 2 with Python 2.7 running on Windows 8. I have installed GraphLab and GraphLab has created a separate environment in Anaconda -- the gl-env environment.
I am facing problems in importing libraries that I have installed successfully via conda through the Windows Commander from the Scripts subdirectory.
For example, I installed wget successfully but when I try to import it from my Jupyter Notebook from the gl-env environment I get an error message that the module does not exist.
I suspect that this error has something to do with the two environments and the PATH content but I do not know enough to figure that out. I made a research in Stackoverflow and it seems that other people have had importing problems relating to different environments but as far as I understand there is not specific advice I can implement.
Your advice will be appreciated.
Try adding a cell in Jupyter directly identifying/appending the path...
import graphlab
graphlab.path.append(path)
...whatever the path may be.
Dunno if you still need it, but for future users, once a new environment is created, once must install the specific libraries and modules one wants accessible in that environment.
In this case, once the gl-env is activated, do a conda install wget again.

jupyter doesn't import numpy after upgrade with anaconda

I updated the packages with conda update --all and was using jupyter to work. Before the update, everything was working, but now jupyter doesn't import any module beside the sys, os, copy and time. Numpy, matplotlib and theano are not being imported. But they are definitely in the conda list... the python version is 2.7.12
When I updated with conda, I remember that there was a message that numpy was being deprecieted due to conflicts. Now in the conda list I have numpy 1.11.1.
I'm new in python, so I don't understand the import error. Before uninstalling everything again, I would like to understand what the problem is to learn and of course to continue using jupyter ;)
I found this post https:// github.com/jupyter/notebook/issues/397 (sorry I can't link it, I'm new here) which seems to be a problem related to mine or similar, but I don't think I understand it so well... so before I break more I wanted to ask here!
Is jupyter badly "connected" to anaconda? How can I check where the packages are being searched? For any comment on this I would be very grateful!!
Here are the cells of jupyter:
cells of jupyter
and the Error I get:
ImportError
Thanks!
That import error is due to change in environment of the jupyter notebook. You might have installed the packages in one environment and you are running the jupyter notebook in another environment.
I have got two environments (envs) in my Anaconda folder. I have Anaconda3 folder to be specific).
(windows key+cmd) -> open the windows command prompt run as administrator.
Activate (name of the environment) -> eg.: activate tensorflow-gpu
Start installing packages using conda install
Note: For each environment you need to install all the packages you want to use, separately using the same process. This solution is for windows users, might work for linux users not sure though.
Additionally to make sure your conda environment is up to date run:
conda update conda
conda update anaconda
For more information: https://pradyumnamajumder.wordpress.com/2017/09/30/solution-to-the-python-packages-import-error-in-jupyter/
I followed the idea as in here and changed the file that launches the root jupyter command (cf. cat /dir_where_installed/anaconda2/bin/jupyter and the jupyter-notebook ( cf. cat /dir_where_installed/anaconda2/bin/jupyter-notebook).
It was set as in the anaconda environment conda info --envs as expected (both files had in the first line something like #! /dir_where_installed/anaconda2/bin/python), but for some reason after the update I did and even after installing again everything(!), jupyter wasn't taking that path, instead it was importing from the 'stock' python (apparently).
Anyway, I changed both lines with #! to take the path as in the output of which python.
Summary:
check path in cat /dir_where_installed/anaconda2/bin/jupyter and cat /dir_where_installed/anaconda2/bin/jupyter-notebook
which python out put should be something like /usr/bin/python
substitute the lines in both files starting with #! with #! /usr/bin/python
I'm not sure if this is a good idea, but it worked for me and now I can import all packages in jupyter. If anyone has any idea if this is a bad idea or a better solution, please let me know!

Categories

Resources