Can't install arcpy in Jupyter Notebook - python

I have ArcGIS Pro installed, which includes the installed Python packages. I am trying to learn Python and would like to use the ArcPy functions. I have never used Jupyter Notebook before, but to get started, I created a new Python 3 file. I entered the code to install arcpy but received an error.
pip install arcpy
ERROR: Could not find a version that satisfies the requirement arcpy (from versions: none)
ERROR: No matching distribution found for arcpy
import arcpy
ModuleNotFoundError: No module named 'arcpy'
I know I am missing a link somewhere, and I am guessing it has to do with environments. How can I reference the arcpy packages that are installed in my ArcGIS Pro program files?

Using "Non-ArcGIS Pro" Conda Environment
I assume that you use an Anaconda or Miniconda installation not provided by ArcGIS Pro, otherwise arcpy should already be available and there is no need to install it.
Note: If you want to use ArcGIS Pro's conda environment, scroll down to Using ArcGIS Pro's Conda Enviroment or Using Jupyter Notebook from within ArcGIS Pro.
To install arcpy, you need to use conda instead of pip. As far as I know arcpy is not available in the Python Package Index (pip's repository).
Initial Setup
Open Anaconda Prompt (or any command prompt if conda can be found through the PATH variable).
First you need a conda environment. Use the following command to create an environment called esri-notebook (can be any name):
conda create -n esri-notebook
Then activate the environment using:
conda activate esri-notebook
Install Jupyter Lab (or Notebook) with:
conda install jupyterlab
conda update --all
Install arcpy with:
conda install arcpy -c esri
Run Jupyter Lab with:
jupyter lab
Or, run Jupyter Notebook with:
jupyter notebook
Your browser will open with Jupyter Lab or Notebook.
Using Jupyter Lab/Notebook
Open Anaconda Prompt (or any command prompt if conda can be found through the PATH variable). Activate the environment:
conda activate esri-notebook
Run Jupyter Lab with:
jupyter lab
Or, run Jupyter Notebook with:
jupyter notebook
Your browser will open with Jupyter Lab or Notebook.
Using ArcGIS Pro's Conda Enviroment
Open Python Command Prompt. arcpy is available (no setup needed).
Then start Jupyter Notebook using:
jupyter notebook
Using Jupyter Notebook from within ArcGIS Pro
You can also create a notebook from within ArcGIS Pro and arcpy should be available to use it without any further setup. Find the Python button within the Analysis ribbon:

Related

conda kernels and conda modules not found in jupyter notebooks when added, but otherwise existant?

There are many questions like this (e.g. Conda environments not showing up in Jupyter Notebook, Python: modul not found after Anaconda installation, https://askubuntu.com/questions/1271137/importing-anaconda-libraries-not-working). I have tried their solutions but to no avail.
In short, I have conda installed. I have installed jupyterlabs and nb_conda_kernels in my base environment. When I create a new conda env via a file, the environment is not listed in jupyter automatically. I can add it via
python -m ipykernel install --user --name <env-name>
but then after I restart jupyter and switch kernels, when I try to import a python module from it, it says module not found.
What do I need to do to fix this? I do not want to add jupyterlabs to every env.
NOTE:
when activating the env in the terminal the path is correct, e.g. /opt/anaconda/anaconda3/envs/<env-name>/bin
when using the env in the terminal, the pack is there.
when using the kernel in Jupyter, the module is not there.

Running multiple Python versions (2.x. 3.y, 3.z) on Jupyter Notebook via multiple kernels

Apologies first-hand if I've done some silly mistake in the below raised issue. I have been stuck on this since quite some time, trying to successfully install multiple Python versions (via separate virtual environments) and run Jupyter notebook with all three versions in Change kernel switch.
AIM:
Setup Anaconda with Python 3.5.6 as default and create two virtual environments with Python 2.7.13 and Python 3.7.3 version and to be able to switch between these three Python versions on a Jupyter Notebook on Windows.
Process Followed: What I did (and ended in mess):
I first successfully installed Anaconda3 with Python 3.5.6 as default (installed in C:\ProgramData\Anaconda3) and set the PATH variables. Jupyter Notebook was up and running with an ipython kernel running from 'base' (or root) from
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
and kernel.json file was also mapped to the correct python version.
.
Then I created my first virtual environment (Python_2.7.13_ENV):
(base) conda create --p C:\ProgramData\Anaconda3\envs\Python_2.7.13_ENV python=2.7.13
and installed jupyter on it
(base) activate Python_2.7.13_ENV
(Python_2.7.13_ENV) conda install notebook ipykernel
(Python_2.7.13_ENV) python -m ipykernel install --p C:\Users\username\AppData\Roaming\jupyter\kernels\ --name Python_2.7.13_ENV --display-name "python_2.7.13"
I used the prefix notation as the default installation syntax was installing it for the root user and I wanted it to install it only for a specific user.
And this worked like a charm. The updated jupyter kernelspec read:
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
python_2.7.13 C:\Users\username\AppData\Roaming\jupyter\kernels\python_2.7.13
and kernel.json file was also mapped to the correct python version ("C:\\ProgramData\\Anaconda3\\envs\\Python_2.7.13\\python.exe")
This was also working fine. I could open a file in jupyter and succesfully switch between the two kernels.
.
Than I followed the same steps for creating my second virtual environment (Python_3.7.3_ENV):
Now, the updated kernelspec read:
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
python_2.7.13 C:\Users\username\AppData\Roaming\jupyter\kernels\python_2.7.13
python_3.7.3 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.7.3
and the kernel.json was also mapped to the correct python version.
Problem:
Both the virtual envs were created successfully.
Now when I run a jupyter notebook and try to switch to Python 2.7.13 kernel, it works fine, but shows a ImportError: DLL load failed (due to some import issue in zmq) on switching to Python_3.7.3 kernel.
However, when I first activate the Python_3.7.3_ENV virtual env and then load the jupyter notebook, I am able to switch between all three Python versions.
Can anybody provide a solution on how to toggle between all three versions without activating the virtual env beforehand if it's possible as I am able to do it with Py 2.7 & Py 3.5 versions.
PS. I have set the 'open with' default on right-click on a ipynb file to jupyter-notebook.exe.
I have similar setting: default python 3.7, env: python 3.6 and python 2.7.
set multiple versions of python/ipykernels running on jupyter notebook
Check you Anaconda version, if it is '>= 4.1.0', it is easier since after 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. If the Anaconda version is lower than 4.1.0 or just want to manually do that, you can reference here.
For python2.7 and adding the ipykernel to jupyter notebook, shown as 'Python (py27)'
conda create -n py27 python=2.7 ipykernel
conda activate py27
python -m ipykernel install --user --name=py27 --display-name "Python (py27)"
For python3.6 and adding the ipykernel to jupyter notebook, shown as 'Python (py36)'
conda create -n py36 python=3.6 ipykernel
conda activate py36
python -m ipykernel install --user --name=py36 --display-name "Python (py36)"
Then we can check if they works well not. You can deactivate from specific env and go back to the base env, and type jupyter notebook (or use graphic shortcut for jupyter notebook anaconda provides)and then you can create 'new' notebook, you can find apart from default Python 3, there also shows Python (py27) and Python (py36).
I was making mistake at the beginning to check which python was running:
Do not just use !python --version in jupyter notebook no matter which version kernel you are using.Because this is just like you running command in base env, it will always show the default env python version, which in my case is python 3.7.x.
What you can do to:
from platform import python_version
print(python_version())
## or this one, which ever you like.
import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)
You will get right python version accordingly.
Show list of jupyter notebook kernels
jupyter kernelspec list (in base env, it will show all kernels name)
If you want to remove specific kernel, you can use:
jupyter kernelspec uninstall <kernel name>
Another way to confirm the python version is right, you can check the ipykernel json file, if the path to start the python is right or not.
To do this: use jupyter kernelspec list, so you can get to know the path of json file. (for example, in my case I can get the path for py27: C:\Users\username\AppData\Roaming\jupyter\kernels\py27). Then, you can go to the dir/path, and you can check what is shown in kernel.json
for the argv, it should show the path to access related python version. For example,
for py27, it will show like "C:\\Users\\username\\Anaconda3\\envs\\py27\\python.exe",
for py36, it will show like "C:\\Users\\username\\Anaconda3\\envs\\py36\\python.exe"
Last thing about setting PATH env variables: do not add Anaconda to the Windows PATH because this can interfere with other software. Instead, open Anaconda with the Start Menu and select Anaconda Prompt, or use Anaconda Navigator. More information can check the Anaconda official docs.
Hope this may be helpful if you meet similar issues.
PS: I was always using Unix like system, not so used to Windows env setting. That is what I met for setting different kernels for different anaconda envs and summarized it. Hope it helps.
After here and there, the only to make this work is having that virtual environment activated.
Following the above steps, I have installed Python 2.7.13, 3.5.6 and 3.7.3 version. My default python is Python 3.5.6 while the other two versions are installed in two virtual environments - Python_2.7.13_ENV and Python_3.7.3_ENV respectively.
How to use them ?
For Python 2.7 and 3.5, just use the Jupyter notebooks normally as you would. Since the default python is set to Python 3.5.6 there is no problem in switching between two versions using Change Kernel option in Jupyter Notebook Toolbar.
For Python 3.7 we first need to activate Python_3.7.3_ENV virtual environemnt and then we can switch between all three versions succesfully using Change Kernel option in Jupyter Notebook.

How to set working directory for tensorflow

I installed tensorflow in my machine using both pip and conda installation. I can import it in my python idle as well as in C:\Users\Anaconda3\envs\tensorflow\Scripts this directory using jupyter notebook. when I want to import in another directory in jupyter notebook, it shows me
ModuleNotFoundError: No module named 'tensorflow'.
How can I import it in my working directory?
From your comments it seems you are running your jupyter-notebook from the wrong conda environment (your base environment rather than the tensorflow one you have created). This is why the packages don't appear to be installed.
Make sure your have jupyter installed in your tensorflow environment and also install nb_conda. You can do that with:
conda install jupyter nb_conda -n tensorflow
Then launch jupyter from within your tensorflow environment and you should see it in the list of kernels

having problems in switching jupyter notebook in different environmnet created using conda

I have made two new environments using conda and installed different dependencies in both environment. After activating one environment and opening jupyter notebook in that environment , I tried to import dependencies that I have installed for that environment but it showed me an error that this module is not present, after this I installed a jupyter notebook separately in that environment using :
pip install jupyter notebook
and error removed this time in that jupyter notebook. Also when I imported matplotlib in base environment it was imported but in new env it was showing no module error. Does it mean that whenever I have more than one environment , I have to install new jupyter notebook in every environment ?
I don't want to use other environment dependencies in current environment but all i am asking is why i am unable to import package in jupyter notebook even though i have installed them in this environment.
Please help me out for this issue. I have wasted my 10 to 15 days understanding this but I didn't find anything.
Please do the following steps:
source activate [your_env_name]
conda install ipykernel
ipython kernel install --name [your_env_name]
Once this is done, launch your python code from your Jupyter Notebook
Select Kernel -> Change Kernel -> [your_env_name]

tensorflow is not working in jupyter notebook

I am a beginner in Anaconda. I am using jupyter notebook. Tensorflow is working on anaconda prompt but not opening in jupyter notebook. how to resolve this problem. Please help me out. I am a beginner.
enter image description here
You created an environment with tensorflow but you didn't install jupyter-notebook in it. You need to install the notebook in the same environment where you installed tensorflow:
On Windows from the anaconda command line you need to activate the environment where you installed tensoflow (in your case from the picture you posted it seems to be called tensorflow). Then install it there. Execute the following commands in the anaconda command line to achieve this and then start the notebook in tensorflow's environment
(base) C:\Users\usr>activate tensorflow
(tensorflow) C:\Users\usr>conda install jupyter
(tensorflow) C:\Users\usr>jupyter-notebook
This should solve your problem.
The python kernel in Jupyter notebook points to the root kernel. If you need a specific environment to be displayed in your Jupyter notebook, do the following
# Creating a custom environment in anaconda prompt with python 3.6
(base)C:\>conda create -n MyEnvironment python=3.6
(base)C:\>activate MyEnvironment
# Install your custom pacakges like tensorflow etc
(MyEnvironment)C:\>pip install tensorflow
(MyEnvironment)C:\>python -m ipykernel install --name MyEnvironment
(MyEnvironment)C:\>jupyter notebook
Now you should be able to see both root kernal and MyEnvironment kernel version in Jupyter notebook

Categories