Jupyter Notebook in Pycharm - not recognising pip installed packages - python

OK, Going nuts here.
I have Pycharm set up - I have an interpreter configured from a virtual env with hdbscan installed using pip
This is configured as the project default for the jupyter server
whenever I fire up my jupyter server by running a cell in my workbook in Pycharm or in an external browser hdbscan won't import and a quick sanity check gives this insanity:
I have checked hdbscan and jupyter are not installed in the global python environment - this makes no sense to me. I have the same problem with several other packages added to the environment (sklearn, plotly). Not sure how pip show can report a package being present that then can't be imported in the same environment. Any suggestions ?

Related

ModuleNotFoundError: No module named 'azure.cognitiveservices.vision.customvision'

I am trying to use the microsoft azure custom vision service on a mac from Jupyter in VS Code
I have Python 3.8.3 installed.
I have done pip install azure.cognitiveservices.vision.customvision and confirmed it is there using pip show.
When I execute the command
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
I get the error:
ModuleNotFoundError: No module named 'azure.cognitiveservices.vision.customvision'
I have tried adding the location where the package is installed to $PATH but that does not fix the problem.
Any thoughts gratefully received! thx
It is recommended that you always create and activate a python virtual environment to work with Jupyter notebooks, like an Anaconda environment, or any other environment in which you've installed the Jupyter package.
To select an environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Once the appropriate environment is activated, you can create and open a Jupyter Notebook and connect to a remote Jupyter server for running code cells. Check Working with Jupyter Notebooks in Visual Studio Code for more info.
This is true for application development in Python in general as well.
A virtual environment is a folder within a project that isolates a copy of a specific Python interpreter. Once you activate that environment (which Visual Studio Code does automatically), running pip install installs a library into that environment only.
When you then run your Python code, it runs in the environment's exact context with specific versions of every library. You can create a requirements.txt file for the libraries you need, then use pip install -r requirements.txt.
Here is a snippet from a sample requirements.txt file:
azure-mgmt-core==1.2.0
azure-mgmt-network==16.0.0
azure-mgmt-resource==10.2.0
If you don't use a virtual environment, then Python runs in its global environment that is shared by any number of projects.
Refer to the Azure SDK for Python Developer docs for more information on configuring your local Python dev environment for Azure.
Whenever U get: ModuleNotFoundError, the simple solution is to install the module using
pip install (module name)
For example, in your case try to run the following line:
!pip install azure
The ! is to run a command in a notebook.

Sometimes Unable to Import NumPy

When I work in Jupyter Notebooks everything works fine, and I can import numpy and pandas successfully. However, when I try to download the script and then run it in an editor such as PyCharm or Atom, I get an import error: no module named numpy, and the same for pandas. How do I fix this? Is this due to the packages being installed in a different location than where I am downloading the code? Everything is installed with Anaconda, and when I try to do ```conda install numpy`` it tells me that all packages have already been installed.
This may be because Pycharm and Atom are using your default python install rather than your anaconda python environment.
You can configure Pycharm to use your conda environment via (https://www.jetbrains.com/help/pycharm/conda-support-creating-conda-virtual-environment.html).
Anaconda uses virtual conda environments to store the Python interpreter and libraries. If this isn't set up in your IDE, it won't see the libraries. This is described in this post: Use Conda environment in pycharm
Check your PyCharm interpreter options: File > Settings > Project > Project Interpreter. Make sure your desired Anaconda interpreter/environment is selected.
If your Anaconda environment isn't selected, click the Project Interpreter drop-down. if you see it there, select it. If not, click Show All... then + (Add) and browse to the Anaconda folder.
This post describes how to set up conda in Atom:
Using anaconda environment in Atom

package installed in anaconda\lib\site-packages and found in spyder, but not in a jupyter notebook

I have installed the azureml package and can see it in . . .anaconda\lib\site-packages:
If I run import azureml.dataprep as dprep in a python script in Spyder (launched from Anaconda Navigator), it works. But, if I open one of my anaconda environments with jupyter notebook and try running the same line of code, I get an error about module not found for azureml:
I thought perhaps the problem was that the package needed to be installed for that specific environment, but azureml is not available as a package for install via the anaconda environments > install packages interface (there is an azure package but not an azureml package).
So, I followed instructions to use conda prompt to install a package into a specific environment. Instructions I followed:
(those are from this link)
And here is the result of following the instructions (it looked like it installed the package into the env):
But, I got the exact same error when trying to import the package in the environment started as a jupyter notebook. Then, I closed anaconda navigator completely just in case, but that also didn't change the result.
Any ideas about what I'm either doing wrong or how I can manually install this package into a specific anaconda environment?
You did the right thing to install the package into the environment. Btw, pip is automatically installed by conda into any environment that has Python, so installing it shouldn't have been necessary.
Are you sure that the environment that you installed into is the one in which your notebook kernel is running? Start the notebook and execute
!conda env list
That will give you a list of environments, and an asterisk * next to the one that is active.
You can also call pip directly from a notebook cell:
!pip install azureml
That will install into the conda environment in which the kernel is running.

Jupyter notebook doesn't see the environment's packages

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.

When using Jupyter, I have to install python modules with Conda and not cmd prompt. Why?

I'm pretty new to programming so forgive my ignorance.
When installing certain python packages/modules in the cmd prompt I am able to import them fine when using Jupyter Notebook. But some modules Jupyter Notebook cannot import without installing via Conda first. Why is this?
The problem seems to be related to system and environment that you are using and not the programming :)
Since you are a beginner, let us understand the concepts first rather than solving the problem.
Python code is run on an interpreter that is installed on your machine.
Jupyter is a web application that takes code using a given language's interpreter. So Jupyter, on its own doesn't run your code. It uses the interpreter installed on a system(your local machine).
Conda is a package manager and also an environment manager. That means using conda, you can create a virtual environment on your machine and that virtual environment can have its own installation of an interpreter. This virtual environment can have its own copy of the packages/modules as well.
Now comes the best part: The jupyter notebook can be asked to use any of the interpreters including the ones installed on a virtual environment.
So most likely, you are running the jupyter notebook from an environment which doesn't have the required dependencies. So either run the jupyter notebook outside the environment or install the required packages in the environment where your jupyter notebook is running.
To know which environment is being used by your jupyter notebook, run below lines from the jupyter notebook cell:
import sys
sys.executable
If you don't get something like /usr/bin/python then the jupyter is running inside an environment. So you have to install all the packages/modules inside that environment only.

Categories

Resources