Working on Jupyter notebooks in VS Code from virtual conda environment

Sriramya Kannepalli
Analytics Vidhya
Published in
4 min readOct 29, 2019

--

Now we can do native editing of Jupyter notebooks from Visual Studio Code, visualize interactive graphs and deploy data science projects from one place!

Photo by Caspar Camille Rubin on Unsplash

When I came across this announcement by Microsoft regarding Native Editing of Jupyter Notebooks in VS Code I got excited to use this feature as you can now directly edit .ipynb files and get the interactivity of Jupyter notebooks with all of the power of VS Code.

Why VS Code ?

“VS Code has a lot of built-in features like debugging, embedded Git control and GitHub, syntax highlighting, intelligent code completion, snippets, and code refactoring. It is very much customizable, allowing users to change the theme, keyboard shortcuts, preferences, and install plugins that add additional functionality. It also has a terminal embedded into it.” — Datacamp

But while trying out this new feature, being new to VS Code and Anaconda Virtual environment, took some time for me to fix environmental issues. I would like to share my experience mainly for people like me who are used to doing data science in jupyter notebooks and want to start using VS Code native editing of jupyter notebooks.

Let’s get started:

  1. Install Anaconda from anaconda.com (If you are confused between Anaconda vs Miniconda vs conda go through the excellent blog by Daniel Bourke. If you already have Anaconda installed in your system you can start from step 3)
  2. If you prefer using a command line interface (CLI), you can use conda to verify the installation using Anaconda Prompt on Windows or terminal.
  3. To open Anaconda Prompt:

Windows: Click Start, search or select Anaconda Prompt from the menu.

Source: anaconda.com

Note: Anaconda prompt comes with Anaconda installation and is different from the regular Windows powershell or command prompt). For detailed instructions check out official Anaconda website.

4. a) If you want to create virtual conda environment/project folder<my-proj> in the base location:

(base)C:\Users\<user-name>

Type in Anaconda prompt :

(base)C:\Users\<user-name> conda create -n my-proj python=3.7 pandas numpy matplotlib scikit-learn jupyter notebook

With the above command we are asking conda to create a virtual environment by name my-proj with a specific version python 3.7 numpy matplotlib scikit-learn and jupyter notebook packages. You can edit the package names based on your project requirement.

Type in the below command to activate the virtual environment.

(base) C:\Users\<user-name> conda activate my-proj(my-proj) C:\Users\<user-name> code

Additional libraries can be added with ‘conda install <package-name>’ or ‘pip install <package-name>’ commands.

5. Add your project folder <my-proj> to the VS Code project workspace.

Note: Ensure Visual Studio Code is installed and Anaconda, Python and Jupyter extensions are enabled. Jupyter Notebook support for visual studio code gives complete details for accessing Jupyter Notebook from Visual Studio Code.This blog is mainly focussed on native editing of jupyter notebook from conda environment.

6. Open terminal from VS Code and ensure you are in in the virtual conda environment. If not activate environment once again —

C:\Users\<user-name>\Anaconda3\envs\my-proj> conda activate my-proj

7. Select interpreter from available interpreters and ensure it is same as the virtual conda environment path that we created above followed by python.exe:

 C:\Users\<user-name>\<my-proj>\python.exe

8. In case you are getting errors, open .vscode folder created in our <my-proj> folder.Verify settings.json ‘python.pythonPath’ dictionary value set to

“C:\\Users\\<user-name>\\<my-proj>\\python.exe” 

If not edit the path to the current virtual environment location followed by ‘python.exe’ and replace ‘\’ with ‘\\’.

9. Create a new jupyter notebook and start coding!!

or

Download plot_iris_dataset.ipynb and upload in <my-proj> folder to test the native editing of jupyter notebooks in Visual Studio Code. If everything works fine you will be able to see the below visualization.

Plot viewer

The Plot Viewer gives you the ability to work more deeply with your plots. In the viewer you can pan, zoom, and navigate plots in the current session. You can also export plots to PDF, SVG, and PNG formats.

Within the Notebook Editor window, double-click any plot to open it in the viewer, or select the plot viewer button on the upper left corner of the plot (visible on hover) — code.visualstudio.com

Disclaimer : It is taking around 2–3 min for starting jupyter server for the first time in virtual environment!!!

Thank you to the following for helping me understand Anaconda, miniconda and Virtual environments in Python and experimenting with Visual Studio Code.

  1. Save the environment with conda (and how to let others run your programs) — Daniel Martin
  2. Get your computer ready for machine learning: How, what and why you should use Anaconda, Miniconda and Conda — Daniel Bourke
  3. StackOverflow Thread
  4. Visual Studio docs
  5. Devblogs.microsoft.com

--

--

Sriramya Kannepalli
Analytics Vidhya

Startup Founder | Machine Learning Engineer | Tech Blogger