Autonomous Econ is a newsletter that empowers economists and policy analysts by equipping them with Python and data science skills. The content is designed to boost their productivity through automation and transform them into savvier analysts.
Upcoming posts will roughly be split between practical guides (80 percent) and data journalism pieces where I demonstrate the tools (20 percent).
If you find the content valuable, please consider becoming a free subscriber by clicking on the button below.
Python is a must-learn language if you want to exploit the full range of data science tools for your projects. With Python, you can automate data plots and prediction models, build interactive dashboard apps (Streamlit), intergrate Large Language Models (LLMs), and access a huge library of data visualization packages.
AI code support via ChatGPT means that you only need a basic understanding of Python to instantly do impressive stuff and automate tedious tasks. Generative AI is improving fast, and knowing how to work with Python puts you in the best place to integrate it into your work.
There are tons of Python ‘get started’ tutorials online, but you might still be asking yourself: What IDE (Integrated Development Environment) should I use? Do I really need a virtual environment? What is the best way to structure and manage projects? These questions can lead to easily avoidable pitfalls, wasted time, or worse—inaction.
Today’s post will outline the baby steps for an efficient and versatile Python setup that you can use for any data-related project. It shouldn’t take more than 12 minutes to install. The setup is accessible for newbies but also allows you to do more advanced stuff later on.
In a following post, I will then take you through how to organize a project with GitHub (cloud code repository) and how to use virtual environments to isolate your project.
Prerequisites
There are no prerequisites, but naturally, the next step would be to do a Python tutorial to learn some basics. There is a link to a previous post below with some learning resources for Python and the key packages. You can also learn with ChatGPT as a tutor and start applying Python to a task or project.
However, this setup will be a prerequisite for some future posts where I will show you how to automate data-related workflows, produce unique data visualizations, and integrate LLMs like ChatGPT into your workflow. Let’s get started!
What is an IDE and why should you use one?
An IDE (Integrated Development Environment) is a software application that provides comprehensive tools for programmers to write, test, and debug code. IDEs are all-in-one toolkits that help make coding easier and more efficient. For those familiar with R, RStudio is an example of an IDE.
From an IDE, you can edit code, run it, and access extensions that make coding a lot easier, such as AI auto-complete (IntelliSense and IntelliCode), dark themes, integrations with GitHub (code repository and version control), and interactive coding with Jupyter.
You also get a much clearer overview of your project when you have a large code repository with many folders. All these features make an IDE much more user-friendly than conducting Python projects in a terminal window.
If you have done any Python tutorial or Kaggle competition before, then you will most likely have already worked with Jupyter notebooks. These are great for one-off data analysis or demos. However, if you want to build a repeatable project that anyone else can reproduce if they have your code (e.g., a self-updating dashboard), an IDE easily allows you to structure and write the code as executable Python scripts.
The typical workflow would be to use Jupyter notebooks as a ‘first shot’ to see if something is viable. For example, I have several notebook templates that you can run on your own data in a Google Colab browser. The next step would be to move to an IDE to write a Python script that makes the execution of the task repeatable (i.e., make it ‘productive’).
Install VS Code and extensions
As someone who has used a range of IDEs, I can tell you that VS Code is the best option. VS Code is free, lightweight, versatile, has an extensive marketplace, and overall has the most intuitive layout.
Simply follow the steps below for a minimal setup:
Download VS Code
Download Python on your PC. There are different installation steps depending if you have Mac or Windows which are outlined here.
Download these extensions from the Extension Marketplace in VS Code:
Python - the basic extension to work with Python effectively in VS Code.
Python Extension Pack - some additional useful extensions like AI-assisted code completion with InetlliCode.
Jupyter - this allows you to edit and run notebooks directly in VS Code. Additionally, it enables you to run code in Python files interactively—more details on that in the next section.
That’s the installation done! Next, there are a few settings I would recommend to make VS Code more user-friendly.
Enable Python interactive window
This was a game changer for working with Python. It allows you to start writing directly in a Python script while having all the interactivity of a Jupyter notebook.
You can right-click on any selected code in your Python file and choose ‘Run in Interactive Window,’ and a window will pop up with the output. Better yet, you can just hit Shift+Enter on any selected code, and the output will appear. You can then do ad hoc inspection of data and plots within the interactive window. See more details of this functionality here.
This is great because it allows you to quickly see what each part of the code is doing as you write, while getting all the benefits of working in an IDE.
To enable this, just search for Jupyter › Interactive Window › Text Editor: Execute Selection and check the box below.
Other recommended VS Code settings
For Windows, set the default terminal profile to ‘Command Prompt’ instead of ‘PowerShell’. ‘Command Prompt’ provides a more straightforward interface for running Python scripts compared to the advanced capabilities of PowerShell. You can find this via the search bar in the Settings tab by typing Terminal Integrated Default Profile: Windows.1
You should also enable the terminal setting that ensures Python scripts are executed in their own directories. Search for Python › Terminal: Execute In File Dir in the Settings tab and check the box.2
Run some code!
Now, create a new Python file and save it in a folder. Run the following code with the ‘play’ button in the top right to check that it works.
msg = "That was easy"
print(msg)
Python becomes more useful once we can utilize various packages like Pandas and Matplotlib. The next post will cover how to create a self-contained project via a virtual environment, install packages, and save it all in the cloud via GitHub.
The Terminal is like a remote control for your computer. When running Python in the Terminal, you can execute Python scripts, install packages, and manage your environment by typing simple commands.
Imagine you have a Python program, process_data.py
, in the scripts folder that needs to open a file from the data folder. If you run the program from anywhere other than inside the scripts directory, it won't find the file because it looks for it in the wrong place. By making sure the program always runs in its own folder (scripts), the program will always correctly find the file in the data folder.