Install graphiz on AWS Sagemaker [duplicate] - python-3.x

This question already has an answer here:
Install graphiz on AWS Sagemaker
(1 answer)
Closed 3 years ago.
I'm on a Jupyter notebook using Python3 and trying to plot a tree with code like this:
import xgboost as xgb
from xgboost import plot_tree
plot_tree(model, num_trees=4)
On the last line I get:
ModuleNotFoundError: No module named 'graphviz'
.........
.........
.........
~/anaconda3/envs/python3/lib/python3.6/site-packages/xgboost/plotting.py in to_graphviz(booster, fmap, num_trees, rankdir, yes_color, no_color, **kwargs)
196 from graphviz import Digraph
197 except ImportError:
--> 198 raise ImportError('You must install graphviz to plot tree')
199
200 if not isinstance(booster, (Booster, XGBModel)):
ImportError: You must install graphviz to plot tree
How do I install graphviz so I can see the plot_tree?

You can install and uninstall packages in your environment on notebook instance using following approach :-
On your jupyter instance, go to the "Conda" tab. There you will find
all the conda environments setup.
Select your relevant environment. After you have selected your option, below in the right panel - you will see all the packages currently installed in your currently selected environment, and in the left panel - you will find what all packages are available and can be added into your environment.
From the left panel, select "graphviz" and then install the selected package to your current environment.
Restart the kernel using "Restart Kernel" option under "Kernel" in your notebook.
Alternatively, you may open a new terminal inside the Jupyterhub Notebook and then
Run conda env list to list all the available environments/kernels
Choose the one you wish to use (e.g. python3) and run source activate python3
Now run the desired pip install ... command
Once this is succeeded. Run source deactivate to close the env. And open up the notebook with the kernel and try import the model just installed.

Related

ModuleNotFoundError when accessing python Flask within jupyter notebook in a virtual env

I have created an virtual env gcloudenv on my nividia nano running ubuntu. I was able to successfully install flask and required libraries and able to deploy my appengine into GCP from this virtual env. All my work is in python and I was using nano as my editor to get my code up and running. No issues so far.
my virtual env gcloudenv already has all the required packages for flask, jinga etc and I can see them when I run pip freeze.
Then I tried to work on Jupyter notebook as my code was getting little complicated and I didn't want to write full code and then run.
I already had jupyter notebook installed before creating the virtual env. I also installed jupyter within in virtual env as well.
So I followed the instruction to create a new kernel by running the following command:-
(gcloudenv) sunny#my-nano:~gcloudenv/MyApp/mainfolder$ pip install ipykernel
(gcloudenv) sunny#my-nano:~gcloudenv/MyApp/mainfolder$ ipython kernel install --user --
name=gcloudenv
Now, I ran the notebook as:
(gcloudenv) sunny#my-nano:~gcloudenv/MyApp/mainfolder$
/home/gcloudenv/bin/jupyter notebook
When trying to import the flask I get the following error:
ModuleNotFoundError: No module named 'flask'
Note sure what is going on as I getting blanked out.
Add
!pip install flask
in the beginning of your Jupyter notebook.
Finally I managed to solve my problem. Thanks to a wonderful post https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/.
In essence, I had 2 problems:-
1. I did not have a jupyter-notebook within my virtual env. Originally I thought I had it installed but that was incorrect. so whenever I tried to launch one, it was picking the first jupyter notebook it could find in the path.
The good way to find out which one is it pointing to , is to run which command
(gcloudenv) sunny#my-nano:~/gcloudenv$ which jupyter-notebook
For me, that was at:
/home/sunny/archiconda3/bin/jupyter-notebook
I had in fact 3 copies of jupyter-notebook on my system. One was probably installed using sudo pip and therefore went into the root folder. Probably not a good thing to do.
So I installed a fresh jupyter-notebook with the following command:-
(gcloudenv) $ pip install jupyter notebook
2.Next is to check list of Jupyter kernels available by running the following from the jupyter notebook ( or from command line):
!jupyter kernelspec list (OR (gcloudenv) $jupyter kernelspec list
My jupyter notebook was not able to import flask libraries because it was pointing to a wrong kernel config outside of my virtualenv gcloudenv.
Available kernels:
gcloudenv /home/sunny/.local/share/jupyter/kernels/gcloudenv ( correct one)
python3 /home/sunny/gcloudenv/share/jupyter/kernels/python3
You can determine which python version it is picking by doing a 'more' on the file:-
(gcloudenv) $
/more/home/sunny/.local/share/jupyter/kernels/gcloudenv/kernel.json
Once I changed my kernel to point to python3 from within the notebook, it picked the correct path and all the relevant libraries I needed.
In summary when you hit the problem as mentioned above, do the following:-
check the path of the python ( whereis python or which python)
check if you are running the 'right' notebook. This is determined by the path and if you have sourced your virtualenv.
Install jupyter notebook using pip from within your virtualenv( do not use sudo)
Check the Jupyter kernel. This may be particularly relevant if you have a common jupyter notebook and you want to work with multiple virtualenv.

Jupyter notebook doesn't find an import when ipython does [duplicate]

I've gotten myself into some kind of horrible virtualenv mess. Help?!
I manage environments with conda. Until recently, I only had a python2 jupyter notebook kernel, but I decided to drag myself kicking and screaming into the 21st century and installed a python3 kernel; I forget how I did it.
My main (anaconda) python defaults to 2.7.
So here I am, merrily trying to use beautiful soup from inside my shiny new python3 kernel, and I don't seem to be able to do anything to get at whatever environment it's finding packages in. Viz (all from notebook):
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
Ok, fine, I'll install it using shell magic. Right? Right?
! pip install bs4
--> Collecting bs4
Downloading bs4-0.0.1.tar.gz
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4 in /Users/[MY-USER]/anaconda/lib/python2.7/site-packages (from bs4)
[...]
Successfully built bs4
Installing collected packages: bs4
Successfully installed bs4-0.0.1
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
Oh no. Does it think I'm in a 2.7 env even though I'm running a python3 kernel? That won't do.
! conda info --envs
--> # conda environments:
#
flaskenv /Users/[MY-USER]/anaconda/envs/flaskenv
mesa /Users/[MY-USER]/anaconda/envs/mesa
py35 /Users/[MY-USER]/anaconda/envs/py35
root * /Users/[MY-USER]/anaconda
Ok, I can fix that. One of those is a 3.5 env.
! source activate py35
--> prepending /Users/[MY-USER]/anaconda/envs/py35/bin to PATH
! conda install beautifulsoup4
--> Fetching package metadata .......
Solving package specifications: ..........
# All requested packages already installed.
# packages in environment at /Users/[MY-USER]/anaconda:
#
beautifulsoup4 4.4.1 py27_0
concerning...
! pip install bs4
--> Requirement already satisfied (use --upgrade to upgrade): bs4 in /Users/[MY-USER]/anaconda/lib/python2.7/site-packages
more concerning...
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
ARRGH!!! headdesk Am I going to have to kill the kernel in order to fix this (and re-run a bit of work)? Is killing the kernel even going to work? How do I get my jupyter kernel to know what environment it's supposed to be running under?
thanks!
This is a tricky part of ipython / Jupyter. The set of kernels available are independent of what your virtualenv is when you start jupyter Notebook. The trick is setting up the the ipykernel package in the environment you want to identify itself uniquely to jupyter. From docs on multiple ipykernels,
source activate ENVNAME
pip install ipykernel
python -m ipykernel install --user --name ENVNAME --display-name "Python (whatever you want to call it)"
If you only want to have a single Python 3 kernel, from the conda environment, just use python -m ipykernel install --user and it will reset the default python to the one in the virtualenv.
And yes, you will need to restart the kernel and re-run the prior steps.
See Also Using both Python 2.x and Python 3.x in IPython Notebook
#tschundler's solution works perfectly if your environment has already been created.
If you want to change the default kernel at the creation of your virtual environment and avoid any manual configuration, you just need to add jupyter at the end of the conda command:
conda create --name ENVNAME python=PYTHONVERSION jupyter
The correct kernel will then be used when you use ipython or jupyter notebook.
In my case somehow jupyter wasn't able to 'pick' the virtual environment's python. So I had to edit ~/.local/share/jupyter/kernels/{my_env_name}/kernel.json
and add path to the interpreter
in the argv key.
There is also an easy way here
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install tornado==4.5.3
pip install ipykernel==4.8.2
You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able to switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
This worked for me. source
pip install --user ipykernel
python -m ipykernel install --user --name=myenv
Output
Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/myenv
and go to above directory
open kernel.json
{
"argv": [
"/home/user/anaconda3/envs/myenv/bin/python", # path to your virtual environment python
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "myenv",
"language": "python"
}
I know this is an old question, but nobody has mentioned nb_conda_kernels, which was made to solve this problem. You must have it installed in the environment from which you launched the running notebook, and any other environment which has kernel/s installed (ipykernel, r-irkernel, etc ) will be available as an option (Start Preferred Kernel):
Sharing my experience here, in case someone made the same mistake:
TL;DR
In VSCode I created the virtual environment from an Ubuntu terminal but forgot to activate the WSL extension too. Once I activated the WSL extension, I was able to change the Jupyterkernel to my virtual environment.
Full explanation
Using VSCode I work with the WSL extension (see here). I created my virtual environment from an Ubuntu terminal [Created a new terminal and selected 'Ubuntu (WSL)'] and then went to my Jupyter notebook to change my kernel, but it did not appear in the list.
I tried the solution proposed above of adding the path to kernel.json in my virtual environment, adapting to my case (I don't use conda) but I was surprised to see that the folder bin in my environment did not have a file named python.
The solution was to activate the WSL extension: lower right corner > click on the arrows (Open a remote connection) > reopen folder in WSL.
After this, I could see in the explorer two new entries in the bin folder for Python and Python3 (the arrow on the right side seem to indicate it's a symbolic link).
Virtual environment now shows the python symbolic links
Then I went to my Jupyter notebook and changed the kernel to my virtual environment.
In the end, I did not have to change the path in kernel.json.
P.S: The above is my understanding of my problem and the solution given my non-existing knowledge about Linux, so any comments to complement/improve this answer are very welcome!

Error: module not found in Jupyter Notebook (sklearn)

I have an issue importing scikit-learn in my Jupyter notebooks, and I am at a loss as to why this is not working. I do not recall having this issue before with other packages. I have seen several threads with people having similar problems with this specific module, but none of the proposed checks have solved my problem, nor revealed to me what might be wrong. I have tried to add as much info as possible below, to try and figure out why this refuses to work.
I use anaconda to manage my packages, and scikit-learn was installed as follows:
source activate python3
conda install scikit-learn
Fetching package metadata .............
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /Users/overaa/anaconda/envs/python3:
#
scikit-learn 0.19.1 py36hffbff8c_0
Now, if I start a python session directly from the command line, import sklearn works as intended and the module is imported correctly. Doing the same in Jupyter results in
ModuleNotFoundError: No module named 'sklearn'
I have checked a number of things. First, making sure that my jupyter and jupyter-notebook point to the correct environment
which jupyter
/Users/user_name/anaconda/envs/python3/bin/jupyter
which jupyter-notebook
/Users/user_name/anaconda/envs/python3/bin/jupyter-notebook
So the notebook checks out.
Then from a command-line python session (in the same environment as where I launch my notebook):
>>> import sklearn
>>> sklearn.__file__
'/Users/user_name/anaconda/envs/python3/lib/python3.6/site_packages/sklearn/__init__.py'
So scikit-learn is also installed in the right environment.
Now comparing that I am running the same version;
From command line:
>>> print(sys.version)
3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
From Jupyter notebook:
print(sys.version)
3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
Comparing the packages path.
From command line:
import site
print(site.getsitepackages())
['/Users/user_name/anaconda/envs/python3/lib/python3.6/site-packages']
From Jupyter notebook:
import site
print(site.getsitepackages())
['/Users/user_name/anaconda/envs/python3/lib/python3.6/site-packages']
Similarly for my sys.path
command line:
>>> print(sys.path)
['', '/Users/user_name/anaconda/envs/python3/lib/python36.zip',
'/Users/user_name/anaconda/envs/python3/lib/python3.6',
'/Users/user_name/anaconda/envs/python3/lib/python3.6/lib-dynload',
'/Users/user_name/anaconda/envs/python3/lib/python3.6/site-packages',
'/Users/user_name/anaconda/envs/python3/lib/python3.6/site_packages/setuptools-27.2.0-py3.6.egg']
Jupyter:
print(sys.path)
['', '/Users/user_name/anaconda/envs/python36/lib/python36.zip',
'/Users/user_name/anaconda/envs/python36/lib/python3.6',
'/Users/user_name/anaconda/envs/python36/lib/python3.6/lib-dynload',
'/Users/user_name/anaconda/envs/python36/lib/python3.6/site-packages',
'/Users/user_name/anaconda/envs/python36/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg',
'/Users/user_name/anaconda/envs/python36/lib/python3.6/site-packages/IPython/extensions',
'/Users/user_name/.ipython']
So everything is the same, except that the directory containing the setuptools differ between the command-line version and the Jupyter version. I also noticed that scikit-learn seems to be installed in the site_packages subdir, rather than in the site-packages subdirectory, which is where Jupyter appear to be looking for modules. So I tried two things. First, adding the path to my JUPYTER_PATH in my .bash_profile before running my notebook.
export JUPYTER_PATH="/Users/user_name/anaconda/envs/python36/lib/python3.6/site-packages:$JUPYTER_PATH"
I also tried to add it to my sys.path directly in the jupyter notebook, prior to trying to import scikit-learn
sys.path.append("/Users/user_name/anaconda/envs/python3/lib/python3.6/site_packages")
Both to no avail, sklearn still refuses to import in my notebook.
Any ideas as to why scikit-learn is not getting imported into my Jupyter Notebook would be highly appreciated. Are there more things I can check? And how do I get this to work?
I have tried activating/deactivating my environment, restart the kernels etc. also without success.
EDIT: I have no problems importing other packages, like numpy or scipy
Turns out that removing the ~/Library/Jupyter/kernels folder and restarting my notebook did the trick. Even though it appears that the kernel in jupyter kernelspec list was what I expected. Nevertheless it is now working. It appears that it is not a sklearn exclusive issue. I basically followed what
this person wrote.
Make sure you have both numpy and scipy installed - scikit-learn depends on them but does not install them for you.
Also, after installing them, restart your notebook server.
There is a simple solution to this, which is to use the Anaconda-Navigator GUI to manually install scikit-learn. So first of all choose an environment you intend to work in or create a new environment other than the base (root) level. Next, start Anaconda-Navigator either by starting the application or by typing "anaconda-navigator" at the command prompt given that the proper environment has been activated. Next, go to the "Environments" window on the GUI by clicking on its icon on the left-hand panel. Then, choose the that higher-level environment you have decided to use earlier. On the right-hand panel, you should now see a list of all installed "conda" software in this environment. After that, choose instead "Not installed" in the field to the immediate left of the "Channels" button at the top of the right-hand panel. Select scikit-learn from the list, and check the box to the left of its name. Finally, click the "Apply" button that shows up at the bottom right corner to install the software. Now you should be all set.
Screenshot of Anaconda-Navigator GUI

PyCharm Import Error: Claims 'matplotlib' is not a package, but works successfully in IDLE

Happy October everyone,
I've successfully downloaded modules before using either the pycharm installer or pip through the command screen, but for some reason when installing matplotlib pycharm cannot recognize it. I've uninstalled and reinstalled, I've installed through both methods, I've followed past similar questions asked on this site which make sure that you have the same interpreter and that it was installed in the right folder (pycharm error while importing, even though it works in the terminal).
So, here's the whole problem. Here's is the simple code, submitted into both pycharm and IDLE:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[2,1,3])
plt.show()
When submitted into IDLE, my plot appears. When submitted into pycharm, the following error appears:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/PythonProject/matplotlib.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\PythonProject\matplotlib.py", line 1, in <module>
import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package
I am currently running Python 3.4, PyCharm 2016.2.3, and my matplotlib folders are indeed in my site-packages folder inside my Python34 folder. Also for further verification:
PyCharm installation
Please help I've become frustrated since this is the only module I've run into trouble with. I've scoured StackOverflow and related websites to help, I've made sure I have all the requirements, etc.
I guess if you named your current writing python module as matplotlib.py.That cause the python load your current writing module instead of the actual matplotlib.py, which triggers an error.
I recommend you to use virtualenv. Is not strictly necessary but is good for dividing your project environments.
This is how I tested matplotlib on my Windows 10 installation, hope it helps.
Be sure that you have the python 3 installation folder listed in your Windows PATH environment variable, should be already listed if you checked "Add Python 3.5 to PATH":
You need also to set the Scripts folder in your PATH environment variable usually should be this path:
C:\Users\<your username>\AppData\Local\Programs\Python\Python35\Scripts
If you don't do that you have to prepend python -m to every command below like this: python -m <command>, so the command below would be python -m pip install virtualenv. I prefer the first solution.
To test matplotlib on Pycharm I've used virtualenv, here is how; first install virtualenv:
pip install virtualenv
Then you create your virtual environment in a folder of your choice, in my case I used python_3_env_00:
virtualenv python_3_env_00
After that you can activate you python 3 virtual environment:
python_3_env_00/Scripts/activate.bat
Now you should see in your command line the active virtual environment (python_3_venv_00), like this:
Now you can install matplotlib:
pip install matplotlib
Fire up PyCharm and add your virtual environment as you project interpreter, go to File->Settings search for Project Interpreter click on the gear icon and Add Local and set the path of your virtual environment, should look like something like this:
Test it:
import sys
print(sys.path)
run this code in where the import worked, and run it in the Pycharm project. Compare the lists. Find out which path that is not present in Pycharm sys.path.
Before importing pyplot, append the missing path to sys.path.
import sys
sys.path.append("the path")
import matplotlib.pyplot as plt
Does this work?
Please follow below steps if you are still getting an error:
If you are using PyCharm, it automatically create virtualenv.
Ensure Scripts path is set into PATH
C:\Users\<Username>\AppData\Local\Programs\Python\Python37-32
Then open PyCharm and go to File-> settings. Search for Project Interpreter. You will see window like this
sample image
Click on setting icon -> Existing Environment -> click on ... give below path
C:\Users\Krunal\AppData\Local\Programs\Python\Python37-32\python.exe
Click on Apply -> ok and you are good to go.
After installing matplotlib When I was trying to use matplotlib.pyplot it was giving error module not found.
I browsed some white papers and found out that we also need to install scipy library to use the matplotlib so I used the below in my command prompt
python -mpip install scipy
Restarted my kernel session.
It worked!!!
I was also facing issue while importing matplotlib but it got resolved and now I am able to use it from pycharm as well.
Please make sure you should have visual c++ 14 installed in your system.
2.If you have more than two python version installed on your system then please install matplotlib from both the version.
Eg. pip install matplotlib
pip3 install matplotlib
If matplotlib is working from python idle then please check whether you are using correct interpreter in pycharm or not and try to choose pythonw.exe path from your installed location.
Hope this will help, Please do let me know if you are still facing issue.
I had similar issue but I solved it very easily on pycharm 2019.3.2. In case anyone looking for an easier solution:
I just opened the terminal window on pycharm and typed pip install matplotlib and it was all good to go. Every project has its own virtual environment. Opening terminal window of IDE cds to project directory by default. So the installing command was enough.

Using TensorFlow through Jupyter (Python 3)

Apologies in advance, I think the issue is quite perplexing!
I would like to use TensorFlow through Jupyter, with a Python3 kernel.
However the command import tensorflow as tf returns the error: ImportError: No module named tensorflow when either Python2 or Python3 is specified as the Jupyter kernel.
I have Python 2 and Python 3 installed on my Mac and can access both
versions through Terminal.
I installed TensorFlow for Python 3, however I can only access it via Python 2 on the Terminal.
As such, this question is really two-fold:
I want to get TensorFlow working with Python3
...which should lead to TensorFlow working with Jupyter on the Python3 terminal.
I had the same problem and solved it using the tutorial Using a virtualenv in an IPython notebook. I'll walk you through the steps I took.
I am using Anaconda, and I installed a new environment tensorflow using these instructions at tensorflow.org. After that, here is how I got tensorflow to work in a Jupyter notebook:
Open Terminal
Run source activate tensorflow. You should now see (tensorflow) at the beginning of the prompt.
Now that we are in the tensorflow environment, we want to install ipython and jupyter in this environment: Run
conda install ipython
and
conda install jupyter
Now follow the instructions in the tutorial linked above. I'll repeat them here with a bit more information added. First run
ipython kernelspec install-self --user
The result for me was Installed kernelspec python3 in /Users/charliebrummitt/Library/Jupyter/kernels/python3
Run the following:
mkdir -p ~/.ipython/kernels
Then run the following with <kernel_name> replaced by a name of your choice (I chose tfkernel) and replace the first path (i.e., ~/.local/share/jupyter/kernels/pythonX) by the path generated in step 4:
mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>
Now you'll see a new kernel if you open a Jupyter notebook and select Kernel -> Change kernel from the menu. But the new kernel will have the same name as your previous kernel (for me it was called Python 3). To give your new kernel a unique name, run in Terminal
cd ~/.ipython/kernels/tfkernel/
and then run vim kernel.json to edit the file kernel.json so that you replace the value of "display_name" from the default (Python 3) to a new name (I chose to call it "tfkernel"). Save and exit vim by typing :wq while in command mode.
Open a new Jupyter notebook and type import tensorflow as tf. If you didn't get ImportError then you are ready to go!

Resources