Cannot use arcgis package in Jupyter notebook - python

I cannot use arcgis package in Jupyter notebook in browser.
I created a virtual environment in Conda, myenv38:
I used Conda install to install arcgis package, so it is in the virtual environment.
However, when I launch Jupyter Notebook from this environment, and select myenv38 as a kernel, arcgis returns ModuleNotFoundError: No module named 'arcgis'.
When I use import sys sys.executable in the iPynb file of the question, it shows a different path ('/Users/myname/.pyenv/versions/3.9.0/bin/python'). This is apparently weird because the virtual environment is configured with python 3.8 to make it compatible with the arcgis.
This doesn't happen in VScode, but I can't use it because the map doesn't show in it.
Do you have 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.

Avoid the creation of multiple Jupyter kernels for each virtualenv

I am working with Jupyter Notebooks and several virtualenvs in Ubuntu 18.04 (I'm not using Conda).
I usually create a new virtualenv for each new python project I'm working on and since I'm working on many projects I would like to AVOID the creation of multiple Jupyter kernels as suggested here.
Instead, I would like to tell Jupyter to start the python kernels in the virtualenv in which I am running it BY DEFAULT.
I know this is possible since it is the way it used to work, but then I don't know what I did wrong and now if I don't create a kernel for each virtualenv Jupyter doesn't allow me to use the python modules I have installed in that environment.
The way it used to work was the following:
I activate the virtualenv
source bin/activate
I installed Jupyter in that virtualenv
I ran Jupyter in that virtualenv
jupyter notebook
I selected Python3/2, R kernel depending on what I needed
I could import all the modules installed in that virtualenv
Now it's not working anymore and if I try to import a module installed in that virtualenv it gives me the following error:
ModuleNotFoundError: No module named 'modulename'
Even if I checked that the Jupyter notebook it's looking in the right paths:
! which python
/home/user/venv_name/bin/python
and
! which pip
/home/user/venv_name/bin/pip
How can I bring back the old setting?

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

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.

How to use CPLEX with PYTHON on windows

Though I've already install Cplex, and could import cplex in Jupyter Notebook.
When I use "cplex.infinity", error arises and it says "module 'cplex' has no attribute 'infinity'"
I don't know the reason, hoping you could help me.
This is because virtual environments in Jupyter are different.If you don't tell your Jupiter kernel which environment it should use it won't use your environment from python terminal (even launching the jupyter notebook command with this virtual environment activated).
You can follow this guide.
Basically:
You have to install your virtual environment for Jupyter
You have to tell the kernel to use this environment in the top bar of your notebook
You will have then all the dependencies installed in your virtual environment available from Jupyter.
Cheers!

Categories

Resources