Toolbox Talk: VSCode and Conda

Toolbox Talk: VSCode and Conda

There have been many rapid changes to how, and where, we work as the world settles into a pandemic-induced hibernation. After coming to terms with the fact that many labs could be closed for several months, life scientists around the world are increasingly resolving to make the most of their ‘downtime’. For some, this means upskilling in leading data analysis tools and techniques – and of course, I whole-heartedly encourage this initiative!

In my experience, this often takes two forms – those who use a course to learn and tackle generic coding projects, and the learn-by-doing camp who google as they go and tackle their own projects. I was a bit of both: using a few tutorials to understand the basic concepts and the remainder by going to work on real analysis problems. With that in mind, I thought it might be helpful to share a little about my setup and why a combination approach is useful (maybe even important) for self-taught data detectives.

One of the issues I have with generic online python courses is their often-overly simplistic approach to interpreted development environments (IDE’s) and package management. In my case, I had been dabbling in coding for a year or two before I came across the concept of virtual environments, and it took me another year or two to get comfortable in making, using and deploying these environments. Ditto with version control. Don’t get me wrong – I think it is great that scientists are diving headfirst into coding, and jumping into a course with a simple editor helps you hit the ground running. Unfortunately, this only defers the learning curve – if you are serious about integrating these tools into your workflow a grounding in the most basic of programming hygiene habits is essential. Shortcuts were necessary in days past, as installing and running python could be … cough … tricky for beginners. However, these processes have come along in leaps and bounds – meaning that it is now incredibly simple to get going with a basic python install in an editor that is easily expandable to incorporate, even encourage, basic code hygiene.

You may have come across my previous Toolbox talk about Atom, Hydrogen and Jupyter – these were the collection of tools I relied on to manage my coding, from writing to running to environments to version control. However, over the last 12 months, I have migrated to Visual Studio Code (VSCode) and Conda. I might be (a little!) biased, but this is a great place to start for anyone new to coding in python!

Conda: easy environment management

Before we dive into an editor, let’s start with creating and managing virtual environments. If you’re still not sure what virtual environments are and why you might need to use them, check out these great tutorials from Real Python and Hitchhiker’s Guide to Python. In essence, virtual environments are essential for keeping your base install of python from becoming littered with packages, many of which might have dependencies that break each other.

Conda solves this problem for python and non-python dependencies alike, and it comes bundled with the ability to install various python versions. In fact, Anaconda and Miniconda have become the most popular Python distributions for data science in research laboratories worldwide. For a more in-depth discussion of these features, and whether you should choose the complete (Anaconda) or lite installation (Miniconda) check out the documentation here.

Installating MiniConda

For many purposes, Miniconda is ample and is easy to download and install by simply following the prompts. Real Python has an extra-detailed walk-through of the installation and starting your first environment in their fantastic guide, including creating your first environment and installing packages within that environment.

Once you’ve created your first environment using the conda create --name=my_env command, install the ipython and jupyter packages using the conda install ipython jupyter command. These packages will enable VSCode to locate and use this environment as a python kernel (more on that later!)

Although technically VS Code is not a dedicated IDE, an abundance of out-of-the-box features supplemented with a few extensions gives you an editor that supports debugging, embedded Git, syntax highlighting, intelligent code completion, snippets, and code refactoring, among an abundance of other features. In short, it is incredibly customisable, user-friendly, language-agnostic, and seamlessly integrates with code hygiene tools to give every budding research software scientist the best start in their programming journey. In case I haven’t convinced you yet, here is a quick run-through from zero to coding hero with VSCode.

Basic installation

Download install VSCode for your operating system here. Once installed, VSCode will open to the Welcome Screen. This is a great place to get familiar with VSCode functionality,including a number of helpful customisation portals (helloooo colour themes and keybindings!). For first-time users, your first stop should be the interactive playground. This window walks through some of the key features of VSCode and gets you started moving around the editor.

Getting started

The next stop is the Extensions tab, which is your gateway to much of VSCode’s fabulous functionality. You can explore the offerings and pick your favourites (there are extensions for almost anything you could want to do!), but the one must-have for our purposes is the Python extension.

python-extension

After installation, open the command palette using ctrl-shift-p and search for settings. In the settings search box, type “python” then scroll through the settings until you find the Python > Data Science: Send Selection to Interactive Window and make sure this setting is ticked. After this, you’re good to go!

python-setting

Piecing it all together: using your conda environment in VSCode

Now let’s get to work running your first python script in VSCode with your brand new environment! If you don’t have an existing python file to test out, not to worry we can make one!

  1. Open a new file within VSCode, and save with a .py extension.
  2. Inside the file, add the following line:
    print(“Hello World”)
  3. Save the file, then place the cursor in the ‘print’ line.

You should see in the bottom left corner of the editor that there is a python version displayed, followed by the name of your current environment. This will likely be the ‘base’ environment to start with. If you click on the environment name, then you should be able to select your new environment from the pop-up list. This will then be used to run the code selection.

  1. To run your code within the built in interactive window, highlight the line and shift+enter

This will send the code to the interactive interpreter, which will open in a new tab within the VSCode window. The kernel may take a little while to start in the first instance, but you should see the Jupyter server details listed in the top right corner – note that it should be the name of your environment. Once it has run, it will look something like this:

hello-world

That’s it! You are all set to run python files for exploratory data analysis in VSCode.

Favourite features

Now we have mastered the basics, you have enough to get going with python in VSCode. A few features that make VSCode my one-stop-shop for all my coding adventures and beyond include:

The python extension itself! It is an evolving masterpiece – huge shout-out to the creators and maintainers – with a multitude of features packed within the interactive window that, frankly, could occupy a post on their own. Highlights include the variable explorer, ability to save the current state of the interactive window directly to a Jupyter notebook, and the ability to render a Jupyter notebook in a standalone tab where you can edit and run code cells without spawning a browser window.

The second important, must-use feature is version control. If you haven’t incorporated version control into your workflow yet, or aren’t familiar with it, you can read more here. In short, even if you have never written a single line of code in your life, version control is a must for anyone routinely creating and editing documents of any kind. And the best news is, VSCode makes it only too easy! You can create repositories, then stage and commit changes from right there in the version control tab. More advanced functionality is also available in the GitLens extension.

Another handy feature is the ability to launch a command-line terminal within VSCode. This can be done via the “Terminal” menu, or keyboard shortcut ctrl+shift+`. As a bonus, if you have an active conda environment, this command will launch a conda terminal with the conda environment pre-activate on the command line. This makes it easy to install new packages and create new environments right from within the editor.

Last but certainly not least is the dynamic documentation display for functions derived from imported packages. For example, when using a the pandas melt function, typing pd.melt() prompts a popup which contains the function definition and variable explanation. This means no more googling docs to check that you have set all the required arguments!

VSCode also features all the best bits of Atom, like being able to set the colour theme and change the layout of windows, and a built-in debugger window that allows you to investigate the state of variable and monitor breakpoints in your code. The more you use and explore, the more functions you will undoubtedly find that make your life better. However, here are a few more of my favourites for inspiration:

Some final thoughts

I’ve gone through a number of IDE’s since my coding journey began. As a general rule, IDE’s suit my style of work better than command-line activities and the ability to run and interact with code and plots in short succession is essential for this. However, a good programmer needs at least some level of experience with the command line, so try not to avoid it! Luckily, VSCode makes transitioning between the command line and interactive windows so easy that there really is no excuse.

When picking an editor, getting comfortable with a single system setup can make it difficult to leap into something new – especially with the time and effort investment required to get everything customised and functional for how you work best. However, it is worthwhile keeping in mind that where the field moves as a whole is often where the greatest level of innovation will be (extensions, support etc). It is good to keep an eye on what the ‘next big thing’ is, but don’t be afraid to wait it out until you are happy with the level of stability and usefulness to make the tradeoff worthwhile.

Last but certainly not least, don’t worry if there were parts of this post that sounded like mumbo-jumbo: everyone is at different stages in their coding journey. One idea I hope I have instilled is that VSCode caters to all skill levels, from the ease of installation and setup to the massive extensibility provided by the extension marketplace. In short, VSCode ticks all of my must-have boxes for an editor and I thoroughly recommend you try it out!


Do you have a favourite IDE for editing python code? Having trouble getting started in VSCode? Or maybe you tried VSCode and found it doesn’t quite suit? Get in touch via the contact page or on twitter – I’d love to hear about your experiences!

Image credits: goshua13 via unsplash