Creating your Data Science workspace in VS Code

Visual Studio Code is a free code editor that you can tailer to your needs. Using packages like the Python Extension for VS Code, GitHub and other useful packages, it’s a lightweight IDE that provides excellent support for running Python in your own custom workspace. In the previous chapter we’ve set up Anaconda and installed VS code.

Open VS Code.

The welcome screen on your first start of Visual Studio Code.

Visual Studio Code is a powerful, lightweight code editor that lets you configure your own workspaces for each of your projects. I’ve created a dummy folder called DataScienceProject for testing purposes.

Click on Open Folder and select the folder. Go to the menu and select File > Save Workspace as Save your Workspace-file within the folder

You now have set up a custom Workspace in VS Code. The great thing about a Workspace, is that you can customize the settings for each individual Workspace.

Now, create a new file called helloworld.py within your Workspace. Open helloworld.py. Copy the code below into your file and save it.

#%%

# This is the first cell of our Python code

print('Hello world!') #%%

# This is another cell

print('Another cell for the world to see!')

Around this time you might get all sorts of messages like ‘Pylint package not installed’, upon opening your file. This is because VS Code will automatically recognize you are editing a Python file. We’ll get into packages in a bit, but first let’s see if we can run that Python file of ours. You can run it directly in the Terminal or from the Interactive Python Window. The Interactive Python Window is extremely useful, since it gives you more feedback on debugging your code, but also allows you to run different bits of codes called cells in your Python script.

To run your script, press shift-enter. You can also right-click the file and select ‘Run Python File in Terminal’ or ‘Run Python File in Interactive Python Window’.

After running your first script, your should see the Interactive Python Window on the right of your code and return something like this.

[1] # This is the first cell of our Python code... Hello world! [2] # This is another cell... Another cell for the world to see!

Congratulations! You’ve just set up a Workspace in Visual Studio Code to run Python projects! Now let’s dig a little deeper and see if we can install new packages into our environment.

Managing packages from within your Terminal

Now that we ran our first script, you might want to add a new package. Let’s say your project requires you to connect to one of the Google API’s. Google provides us with a package to do this, but these are not installed in your default environment. Luckily, there are a lot of packages available to us. Anaconda has its own package repository and there are many more repositories for us to find our packages. The package we are looking for in our example is the Google API Python Client. Go ahead and follow these steps.

Open the Terminal. Make sure you are working in your base environment. The Terminal should tell us so by showing you something like this:

(base) myMac:DataScienceProject myUser$

Check whether the package already is installed by entering the following command in the Terminal:

conda list

This will return a list of packages that are currently installed in your base (root) environment. Now install the package by running the following command into your Terminal:

conda install -c conda-forge google-api-python-client

The package will now be installed on your base environment. If all goes well, you should see the following message in your Terminal. I didn’t copy all of the message, but this should give you an idea.

Collecting package metadata (current_repodata.json): done

Solving environment: done ## Package Plan ## environment location: /Users/myUser/anaconda3 added / updated specs:

- google-api-python-client The following packages will be downloaded ...

...

... Proceed ([y]/n)? y ... Preparing transaction: done

Verifying transaction: done

Executing transaction: done

Awesome! We’ve successfully installed a new package within our environment. This will allow you to import the package libraries and use the Google API Python Client from within your scripts.

But what if you already have package running in the base environment and you don’t want to risk messing up your current environment settings? You can use a new environment and install different packages for that environment as well. We now know how to install a package, but let me show you how to change your Python environment from within VS code as well.

Managing your Python environments from within your Workspace

Besides from working in your own custom Workspace, you can manage your Anaconda environment from within the editor itself. This way you don’t have to run Anaconda Navigator over and over again, but simply run a Python environment straight out of the editor so you can keep on coding.

Have you noticed the blue bar in the bottom of the editor? This bar gives you information on the code you’re working on. On the far left of the bar, you see the interpreter you are currently using. In my case it uses:

Python 3.7.3 64-bit ('base':conda)

As you can figure, I’m running Python 3.7.3 in the base (root) environment from Anaconda. It also shows you if there are any problems in your code, how many lines, columns, spaces there are, which encoding you currently have selected and which language your are programming in.

By clicking on the interpreter you can select other interpreters. For example, the Python environment we’ve created earlier in Anaconda.

Click on your interpreter and select the interpreter we created earlier.

How to select a different Python interpreter.

Now, when you switch from your Base-interpreter to a new interpreter, sometimes the Jupyter-server has trouble starting. The Jupyter-server runs on a kernel which is somewhat of an engine for your Python environment. The Jupyter-kernel is vital to running your code inside VS Code, especially running code in the Interactive Python Window. If you happen to get these errors, try the following in the Terminal:

For macOS:

source activate <environmentnamehere>

pip install ipykernel

python -m ipykernel install --user

For Windows:

activate <environmentnamehere>

pip install ipykernel

python -m ipykernel install --user

This will install a kernel within your environment specifically. Restart your VS Code editor and try running your code in the newly selected interpreter (python37:conda).

If all goes well, then congratulations are in order! You’ve successfully set up your own Workspace in Visual Studio Code which you can now use for your Python projects!

Closing thoughts

Managing your Python environments can be a pain in the butt. To understand how to manage your environments and your packages gives you a lot of flexibility and prevents a lot of stress when one of your environments suddenly stops working. This is why I wanted to show you how to switch environments and install packages, because these are the types of errors you are prone of getting.

Of course I haven’t shown you everything you can do with Visual Studio Code or Anaconda, which is why I would suggest you also read up on the following articles:

I hope you have found this guide to be of help to you. Happy coding!