By James Bell
This article covers how to create a virtual Python environment and run a jupyter notebook inside it. This is a great idea when you have problems installing packages, or need a test or isolated dev environment. You can see what breaks when you upgrade libraries and versions in Python before learning it in a live production environment.
It’s also nice to use when some libraries just don’t want to install, like plotly did for me recently.
Below you’ll see the error I received when I followed some simple instructions and attempted to import a couple of packages.
Importing packages can seem so easy but they can also be extremely frustrating when they fail. It’s especially frustrating because plotly is such a beautiful and interactive visualization library.
These instructions may not work for you. Let’s be honest, programming and dealing with this kind of stuff can be tough but it’s very rewarding when it works. For full disclosure I have a Windows 10 laptop with an anaconda installation and Python 3.5. If you have a Mac or a Linux machine, google and stack overflow are your friends.
First, let’s create a folder where we want our virtual environment to live in.
I opened File Explorer in windows and created a folder called Virtual_Dev. Try to leave out spaces and use underscores, camelcase, or some other method for naming the folder.
Now, let’s open command prompt.
If you’ve never seen or used the command prompt before, don’t panic. You really only need to know 2 things listed in the picture below.
Now let’s change our directory in Command Prompt to the folder that we want our environment to live. We do this with the command “cd” or change directory, then type the filepath where you created your folder in the previous step. After you type in the command below, press the enter key to execute the command.
Now we create the virtual environment.
You can also change the python interpreter by instead typing the version in such as
virtualvenv venv -p /usr/bin/python3.1 venv
Let’s activate the environment.
You’ll see the prompt change like this.
Next, we install the kernel that will run the jupyter notebook inside our new environment by typing in just the top line.
Then, just type jupyter notebook.
There you go! “Localhost” is the same as saying “your computer”. The 4 digit number after is the port number. Don’t expect a localhost url to work on anyone else’s computer.
Then open a new notebook in the virtual environment to start coding in jupyter notebook.
Note that since this is a virtual environment, you’ll have to install new libraries. You can do this on your command prompt where the kernal is running. This allows you to test different versions of libraries and do other types of testing in an isolated environment.
Cufflinks will uninstall whatever version you previously have of plotly and install plotly version 3.10.0. You can install plotly with a different version after cufflinks does this and specify a different version. Here I am specifying plotly version 4.1.0
This will overwrite the plotly version that cufflinks installed. This is how I was able to get it to work. Hopefully this works for you!! If not, stack overflow, google, patience, and utilizing your resources is your best hope. Good Luck!
Oh, and by the way, if you are using this to install plotly and cufflinks, here are the import lines I ran to get going.
15 March 2020
15 December 2019
14 November 2019
07 November 2019
12 October 2019
26 September 2019
11 September 2019
Powered By Impressive Business WordPress Theme