Python Vibe Coding Course — Part 4
Preserve your environment with requirements.txt
By the end of Part 3, your analysis runs on your own PC. The next important step is to leave the environment in a form that third parties — co-authors, reviewers, or your future self — can reproduce. In Python, the standard approach is to export your libraries and their versions into a single text file called requirements.txt. This part explains how to create it and how others can use it to replicate your setup.
As explained in Part 1, Python code only runs when the Python version and the set of library versions all match. Code you wrote using pandas 2.2.0 may not work in someone else's environment that only has pandas 1.5.0.
requirements.txt is a text file that lists each library you used along with its version, one per line. With this file, anyone who receives it can install exactly the same versions of all libraries with a single command.
pandas==2.2.0
openpyxl==3.1.2
matplotlib==3.8.2
numpy==1.26.3
seaborn==0.13.1
With your virtual environment active (the one you created in Part 2 as .venv), run the following command in the terminal.
Open a terminal in VSCode (top menu: Terminal → New Terminal), then run one of the following:
· Windows (PowerShell): .venv\Scripts\Activate.ps1
· Windows (Command Prompt): .venv\Scripts\activate.bat
· Mac / Linux: source .venv/bin/activate
When (.venv) appears at the start of the prompt, the virtual environment is active.
With the virtual environment active, run the following command:
pip freeze > requirements.txt
When the command completes successfully, a file named requirements.txt will appear at the root of your project folder. Open it and you will see all libraries currently installed in your virtual environment, each listed with its version.
my_first_analysis/
├── .venv/
├── analysis.ipynb
├── sample_data.xlsx
├── results/
└── requirements.txt # ← newly created
Whenever you install a new library as your analysis grows, you will need to update requirements.txt. Make a habit of running pip freeze > requirements.txt one more time at the very end.
For a co-author or reviewer to reproduce your analysis, the folder you hand over plus some instructions is all that's needed. Let's start by clarifying what to include.
my_first_analysis/
├── data/ # Raw data (shareable items only)
├── analysis.ipynb # Analysis script
├── results/ # Output figures/tables (include for reference)
├── requirements.txt # Created in Step 2
└── README.md # Instructions (created in Step 4 below)
Note: the .venv/ folder is not included. The recipient will regenerate it on their own PC.
You can share the folder however you like: zip it and send by email, share via cloud storage, or publish on GitHub or Zenodo as covered in Part 5.
The person who receives your folder first checks the Python version written in README.md, then verifies whether that version is installed on their PC.
python --version (on Windows with multiple versions installed, try py --list). If it is missing, download and install the required version from python.org/downloads.# Windows — using Python 3.12
py -3.12 -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
# Mac / Linux — using Python 3.12
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
The first line creates a virtual environment with the version specified in README.md. Omitting the version number would use the system default Python, which may behave differently. The final pip install -r requirements.txt installs all libraries at the exact versions listed.
The recipient can now reproduce the exact environment you ran analysis.ipynb in. Running the code will produce the same figures and statistics — that is reproducibility.
It is strongly recommended to include a README.md file in your folder. As the first thing a third party will read, it should contain content like the following:
# Analysis Package for [Your Study Title]
## Setup
```
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\Activate.ps1
pip install -r requirements.txt
```
## Running the Analysis
Open `analysis.ipynb` in VSCode and run the cells from top to bottom.
Results will be saved to the `results/` folder.
## Python Version
Tested on Python 3.12.
README.md can be created and saved as a plain text file in VSCode or any text editor.
The symbols like # and ``` in the example above follow a formatting convention called Markdown. By adding a few simple symbols to plain text, you can express headings, code blocks, lists, and other formatting in a lightweight way. Services like GitHub, Zenodo, Notion, and Slack all support Markdown, and README.md files are automatically rendered on GitHub.
For a full reference, see the supplementary article "Markdown Quick Reference". At minimum, just knowing # (headings) and ``` (code blocks) is enough.
requirements.txt is powerful, but there are a few things to keep in mind.
requirements.txt only tracks libraries; the Python version is not included. Be sure to write "Tested on Python 3.12" (or whatever version you used) explicitly in README.md.
pip freeze exports every dependency, so the file can become lengthy. If that bothers you, ask an LLM to help you trim it down to only the libraries you directly import.
That said, using the output of pip freeze as-is is perfectly fine for a start. Don't aim for perfection — just make sure you have a requirements.txt at all.
When sharing your folder on GitHub, it is standard practice to exclude the .venv folder. It should be regenerated by the recipient, and it is also quite large. To exclude it, create a file named .gitignore at the root of your project folder with the following contents:
.venv/
__pycache__/
*.pyc
.ipynb_checkpoints/
With this file in place, these files and folders will be automatically excluded when you upload to GitHub. Part 5 covers this in more detail.
pip freeze > requirements.txtpip install -r requirements.txtIn the final Part 5, we'll cover how to publish the folder you've built to Zenodo or GitHub, and add the link to your paper's Appendix to ensure full reproducibility of your research.