Setting up your own Data Science workspace with Visual Studio Code and Anaconda (Python) | by Christiaan Dollen | Towards Data Science

If you’re just starting out in the field of data science, creating a personal workspace will help you keeping your projects organized. There are lots of different tools to use and if you’re just like me, starting out in the field, you might find it daunting to dive right in. In this post I’ll show you the basics of how you can set up your own workspace on macOS with some of the most commonly used tools in the trade. While I use macOS in this guide, the steps are almost identically for Windows platforms and I expect you should have no trouble using this guide on Windows. At the end of this guide you’ll be able to:
Set up a Python environment with Anaconda.
Create a Visual Studio Code workspace and run Python scripts.
Install packages and managing different Anaconda environments.
Let’s get you started!
Anaconda is a free distribution of Python and R, mostly used for the application of data science, machine learning, data processing, analytics et cetera. It lets you manage your own environments and packages that you would use in your projects. We’ll be using Anaconda for our package management and deployment. To build our workspace, we need to install and configure Anaconda. We will be following these steps:
Install Anaconda.Create a new environment with the latest version of python.Installing packages into your environment.
Install Anaconda.
Create a new environment with the latest version of python.
Installing packages into your environment.
Go to the Anaconda website and download the latest version of Anaconda for your platform. You will be able to download Anaconda with Python 3.7 and Python 2.7. Although I personally prefer to get the latest version of Python, sometimes Python packages require a specific version of Python — hence the reason why I will show you how to set up multiple Python environments. We will use Python 3.7 for now, just to be sure. After installing Anaconda, start Anaconda Navigator.
Upon opening the Anaconda Navigator, you see Anaconda has already set up some tools and packages like Jupyter, Spyder. Notice there’s an option to install VS Code.
Click to Install.
In the meantime, navigate to Environments in the menu on your left. In here you will see that Anaconda has already configured a base (root) environment for you to work with out of the box.
From here you can select the environment you want to run. You can also directly run bash commands from the Terminal.
There are a lot of packages already installed for you, but what if you want to install new packages? Or maybe you want to install different packages for different versions of Python? This is why you want to work with environments. Notice in the screenshot above I’ve already set up an environment like this called python37.
Click on Create and configure a new Python environment.Select the latest version of Python and confirm by clicking on Create again.
Click on Create and configure a new Python environment.
Select the latest version of Python and confirm by clicking on Create again.
It will take a couple of seconds before the environment is configured. In a couple of minutes you’ll notice a new environment is available with a handful of default packages installed. Once you have set up your Python environment, you’ll mostly use the Terminal for installing packages on the go (which I’ll show you later on) and you will probably use the Anaconda Navigator less.
Great! Essentially, this is al you need to start to start using Python on your machine with Anaconda. Now all we need is a workspace to actually use our Python environment, so we can run our scripts with our packages. You can use Jupyter Notebook which comes with Anaconda, but I like to use VS Code and I’ll explain shortly why.
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.
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 asSave your Workspace-file within the folder
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.
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 codeprint('Hello world!')#%%# This is another cellprint('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.
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): doneSolving environment: done## Package Plan ##environment location: /Users/myUser/anaconda3added / updated specs:    - google-api-python-clientThe following packages will be downloaded.........Proceed ([y]/n)? y...Preparing transaction: doneVerifying transaction: doneExecuting 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.
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.
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 ipykernelpython -m ipykernel install --user
For Windows:
activate <environmentnamehere>pip install ipykernelpython -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!
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:
Best packages for data science with Python
An extensive list of extensions for Visual Studio Code
Using Version Control with GitHub for Visual Studio Code
I hope you have found this guide to be of help to you. Happy coding!