Skip to Main Content

Python in Digital Scholarship

This guide will provide an introduction to using Python in research and instruction and what resources are available in the Freedman Center.

Visual Studio Code (VS Code) for Python Development

Visual Studio Code (VS Code) is a popular free, cross-platform code editor that is well-suited for Python development. VS Code is lightweight yet highly customizable through extensions. By installing the official Python extension (developed by Microsoft), VS Code turns into a powerful Python integrated development environment (IDE) with features such as syntax highlighting, code completion (IntelliSense), real-time error checking (linting), and an interactive debugger.

Key features of using VS Code for Python

  • Editing and Code Intelligence: VS Code provides a modern editor with support for Python syntax, indentation, and code formatting. The Python extension offers intelligent code completion and quick navigation to definitions, which is helpful for writing and understanding code.

  • Integrated Terminal: VS Code includes an integrated terminal so you can run Python scripts or use command-line tools (like pip or version control commands) without leaving the editor.

  • Debugging: The editor has a built-in debugger for Python. You can set breakpoints, step through code, inspect variables, and diagnose issues in an interactive way, which is invaluable for complex research code.

  • Jupyter Notebooks Support: VS Code can directly open and run Jupyter Notebooks (.ipynb files) with its built-in support. This means you can combine interactive code execution (common in data analysis workflows) with VS Code’s interface, allowing you to work with notebooks or scripts in one place.

  • Version Control Integration: VS Code integrates with Git, making it easy to track changes in your code, which is important for reproducible research. You can commit and push your code to platforms like GitHub directly from the editor.

If you are working in the Freedman Center, VS Code and Python are already installed on the computers. If you want to install Python and VS Code on your system. You can follow the Instructions here:

Virtual Environments and Package Management 

When working on multiple projects, it’s crucial to isolate project dependencies to avoid conflicts. Python’s solution for this is the use of virtual environments. A virtual environment is an isolated Python environment (with its own interpreter and installed packages) that is separate from the system-wide Python installation. By using virtual environments, one project’s libraries won’t affect or clash with those of another project-. This is especially important in research computing, where different projects may require different library versions.

Python’s built-in venv module can create virtual environments easily. It’s recommended to create a new environment for each project, particularly when using external libraries. Below are the typical steps to create and use a virtual environment using venv and pip for package installation:

# 1. Create a virtual environment (using Python 3)
$ python3 -m venv myenv

# 2. Activate the virtual environment
$ source myenv/bin/activate        # On Linux/Mac
# On Windows, use: myenv\Scripts\activate

# 3. Install packages within the activated environment
(myenv)$ pip install numpy pandas  # example: installing NumPy and Pandas

After activation, the command prompt is usually prefixed by the environment name (e.g., (myenv)), indicating that the environment is active. Activation modifies your PATH so that the python and pip commands refer to the ones inside the virtual environment. All packages installed via pip will go into this environment’s directories, keeping them separate from other projects. You can verify you’re using the environment’s Python by checking which python / where python after activation.

When finished or if you want to switch projects, you can deactivate the environment by running the deactivate command, which returns the shell to the system’s default Python. It’s good practice to document your project’s dependencies (for example, by listing them in a requirements.txt file via pip freeze > requirements.txt). This allows others to replicate your environment for reproducibility.

Virtual environments are an essential tool for maintaining clean and reproducible Python setups in academic projects. They prevent “dependency hell” and ensure that running code (e.g., for a paper or assignment) yields consistent results on different machines. Tools like pip (Python’s package installer) are used within these environments to add or remove libraries as needed. (Alternative environment managers like conda can also be used, especially if you prefer the Anaconda distribution, but the concept of isolation remains the same.)