Skip to main content
RapidDev - Software Development Agency
replit-tutorial

How to set up Python virtual environments in Replit

Replit automatically creates a Python virtual environment for every Python project, but you may need to manage it manually when dealing with specific package versions, poetry-based projects, or Nix-level Python version changes. Use pip for simple installs, poetry for dependency locking, and the replit.nix file to switch Python versions at the system level.

What you'll learn

  • How Replit's built-in Python virtual environment works behind the scenes
  • How to install and manage packages with pip and poetry
  • How to change the Python version using replit.nix
  • How to troubleshoot common environment-related errors
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Intermediate8 min read20 minutesAll Replit plans. Python 3.10+ via default Nix module. Poetry and pip available in Shell.March 2026RapidDev Engineering Team
TL;DR

Replit automatically creates a Python virtual environment for every Python project, but you may need to manage it manually when dealing with specific package versions, poetry-based projects, or Nix-level Python version changes. Use pip for simple installs, poetry for dependency locking, and the replit.nix file to switch Python versions at the system level.

Manage Python environments and dependencies in Replit

Replit handles Python virtual environments differently from a local development setup. Every Python Repl automatically creates a venv in a hidden directory, and pip installs go into that environment by default. However, things get more complex when you need a specific Python version, locked dependencies with poetry, or system-level packages via Nix. This tutorial covers all three layers of Python environment management in Replit so you can avoid the ModuleNotFoundError and version mismatch issues that commonly frustrate Python developers on the platform.

Prerequisites

  • A Replit account with a Python project (any plan)
  • Basic familiarity with Python imports and pip install
  • Access to the Shell pane (Tools dock > Shell)
  • Understanding of why virtual environments exist (dependency isolation)
  • Knowledge of how to show hidden files in Replit (filetree menu > Show hidden files)

Step-by-step guide

1

Understand Replit's automatic virtual environment

When you create a Python Repl, Replit automatically sets up a virtual environment in a hidden directory. This venv is activated by default whenever you use the Shell or Run button, so pip install commands install packages into the project's isolated environment rather than the system Python. You can verify this by running which python in the Shell, which should point to a path inside your project rather than /usr/bin/python. This automatic setup means you do not need to run python -m venv or activate scripts like you would locally.

typescript
1# Run these in the Shell to inspect your environment
2which python
3python --version
4pip list

Expected result: The Shell shows that python points to the project's virtual environment, not the system Python. pip list shows only packages installed in this project.

2

Install packages with pip

The simplest way to add Python packages in Replit is pip install in the Shell. Replit also auto-installs packages when it detects import statements in your code, but this automatic installation can be unreliable for packages with complex names or optional dependencies. Always verify installation by importing the package in the Shell. For reproducibility, create a requirements.txt file so your dependencies are documented and can be reinstalled if the environment resets.

typescript
1# Install a single package
2pip install requests
3
4# Install from a requirements file
5pip install -r requirements.txt
6
7# Create a requirements file from current environment
8pip freeze > requirements.txt
9
10# Install a specific version
11pip install flask==3.0.0

Expected result: Packages install into the project's virtual environment. Running pip list shows the newly installed packages.

3

Use poetry for dependency management

Poetry is a more robust dependency manager that creates a poetry.lock file for deterministic installs. Replit supports poetry natively. If your project already has a pyproject.toml file, poetry is likely already configured. Poetry resolves dependency conflicts automatically and separates development dependencies from production ones. Use poetry add to install packages and poetry install to restore the environment from the lock file.

typescript
1# Install poetry if not already available
2pip install poetry
3
4# Initialize a new poetry project
5poetry init --no-interaction
6
7# Add a package
8poetry add requests
9
10# Add a dev-only package
11poetry add --group dev pytest
12
13# Install all dependencies from lock file
14poetry install
15
16# Run a script through poetry's environment
17poetry run python main.py

Expected result: Poetry creates pyproject.toml and poetry.lock files. Packages are installed and available for import.

4

Change the Python version via Nix

Replit uses Nix for system-level configuration, including the Python version. To switch Python versions, you need to modify the replit.nix file (show hidden files first). Change the Python package reference to the version you need. After saving, run exit in the Shell to reload the Nix environment. Available Python packages include pkgs.python310, pkgs.python311, and pkgs.python312. Note that changing the Python version may require reinstalling your pip packages.

typescript
1# replit.nix change the Python version here
2{ pkgs }: {
3 deps = [
4 pkgs.python311
5 pkgs.python311Packages.pip
6 ];
7}

Expected result: Running python --version in a new Shell session shows the updated Python version. You may need to reinstall packages with pip install -r requirements.txt.

5

Handle system-level dependencies with Nix

Some Python packages require system-level libraries that are not included in Replit's default environment. For example, Pillow needs image processing libraries, and psycopg2 needs PostgreSQL client libraries. Add these system dependencies to replit.nix alongside your Python packages. Search for available packages at search.nixos.org/packages. This is the most common cause of 'failed building wheel' errors during pip install.

typescript
1# replit.nix with system dependencies for common packages
2{ pkgs }: {
3 deps = [
4 pkgs.python311
5 pkgs.python311Packages.pip
6 pkgs.zlib
7 pkgs.libjpeg
8 pkgs.libpng
9 pkgs.postgresql
10 ];
11}

Expected result: After reloading the Shell with exit, pip install commands for packages like Pillow and psycopg2 succeed without 'failed building wheel' errors.

6

Troubleshoot common environment errors

The most frequent Python environment errors in Replit are ModuleNotFoundError (package not installed or wrong environment), version conflicts between packages, and 'failed building wheel' (missing system dependencies). When you encounter these, first verify your environment with which python and pip list. If packages seem to disappear, the virtual environment may have been corrupted. The nuclear option is deleting the .pythonlibs directory and reinstalling everything from requirements.txt.

typescript
1# Diagnostic commands for environment issues
2which python
3python --version
4pip list
5pip check # verify no broken dependencies
6
7# Nuclear reset: delete and recreate the environment
8rm -rf .pythonlibs
9pip install -r requirements.txt

Expected result: After running diagnostics, you can identify whether the issue is a missing package, wrong Python version, or corrupted environment, and apply the appropriate fix.

Complete working example

setup_environment.sh
1#!/bin/bash
2# Python environment setup script for Replit
3# Run this in the Shell after creating a new Python project
4
5echo "=== Python Environment Setup ==="
6echo ""
7
8# Check current Python version
9echo "Python version:"
10python --version
11echo ""
12
13# Check pip
14echo "pip version:"
15pip --version
16echo ""
17
18# Check virtual environment
19echo "Python path:"
20which python
21echo ""
22
23# Install from requirements if the file exists
24if [ -f "requirements.txt" ]; then
25 echo "Installing from requirements.txt..."
26 pip install -r requirements.txt
27else
28 echo "No requirements.txt found. Creating one..."
29 echo "# Add your project dependencies here" > requirements.txt
30 echo "# Example:" >> requirements.txt
31 echo "# requests==2.31.0" >> requirements.txt
32 echo "# flask==3.0.0" >> requirements.txt
33 echo "Created requirements.txt template."
34fi
35
36echo ""
37
38# Verify no broken dependencies
39echo "Checking for dependency conflicts..."
40pip check
41
42echo ""
43echo "=== Environment Ready ==="
44echo "Installed packages:"
45pip list --format=columns

Common mistakes when setting up Python virtual environments in Replit

Why it's a problem: Running python -m venv to create a manual virtual environment, which conflicts with Replit's built-in venv

How to avoid: Do not create your own venv. Replit automatically manages a virtual environment for every Python project. Use pip install directly.

Why it's a problem: Editing replit.nix but not reloading the Shell, so the old Python version stays active

How to avoid: After changing replit.nix, type exit in the Shell to close it, then open a new Shell session. Verify with python --version.

Why it's a problem: Not adding workspace secrets to the deployment configuration, causing os.getenv() to return None in production

How to avoid: Secrets must be added separately in the Deployments pane. Workspace secrets do not automatically carry over to deployments.

Why it's a problem: Installing packages globally with sudo pip install, which does not work in Replit's sandboxed environment

How to avoid: Use pip install without sudo. Replit's environment does not support sudo. Packages install into the project's virtual environment by default.

Why it's a problem: Deleting the .pythonlibs directory without having a requirements.txt, losing all installed packages

How to avoid: Run pip freeze > requirements.txt before deleting .pythonlibs. Then reinstall with pip install -r requirements.txt.

Best practices

  • Always maintain a requirements.txt or pyproject.toml file so your environment can be reproduced from scratch
  • Use pip freeze > requirements.txt after installing new packages to keep the file up to date
  • Pin package versions in requirements.txt (e.g., requests==2.31.0) to avoid unexpected breaking changes
  • Add system-level dependencies to replit.nix rather than trying to install them with pip
  • Run exit in the Shell after modifying replit.nix to reload the Nix environment
  • Use poetry for projects with complex dependency trees that need deterministic resolution
  • Check the Resources panel if pip install hangs — large packages can consume significant RAM on the free tier
  • Keep your requirements.txt clean by separating development dependencies (pytest, pylint) from production ones

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I'm getting a ModuleNotFoundError for [package name] in my Replit Python project even though I ran pip install. My Python version is [version] and my replit.nix includes [paste contents]. How do I fix this?

Replit Prompt

Set up the Python environment for this project. Install all packages listed in requirements.txt, verify there are no dependency conflicts with pip check, and make sure the Python version in replit.nix matches what the project needs. If any packages fail to install, add the required system dependencies to replit.nix.

Frequently asked questions

Yes. Every Python Repl has an automatic virtual environment. Packages installed with pip go into a hidden .pythonlibs directory. You do not need to create or activate a venv manually.

Edit the replit.nix file (enable Show hidden files first) and change the Python package to the version you need (e.g., pkgs.python311 or pkgs.python312). Then type exit in the Shell and open a new session.

The package requires system-level libraries not included in Replit's default environment. Add the needed dependencies to replit.nix. For example, Pillow needs pkgs.zlib and pkgs.libjpeg. Search for packages at search.nixos.org/packages.

Use pip for simple projects with a few dependencies. Use poetry for larger projects that need deterministic dependency resolution, lock files, and separation of dev and production dependencies.

This can happen if the virtual environment was corrupted or if a Nix environment change reset the Python installation. Always maintain a requirements.txt file and run pip install -r requirements.txt after any environment reset.

Yes. RapidDev's engineering team can help resolve tricky dependency conflicts, Nix configuration issues, and deployment environment mismatches that go beyond basic pip install troubleshooting.

Store a personal access token in Tools > Secrets as GIT_TOKEN. Then install with pip install git+https://${GIT_TOKEN}@github.com/user/repo.git in the Shell.

replit.nix manages system-level packages (Python itself, C libraries, system tools). requirements.txt manages Python-level packages (Flask, requests, etc.). Both are needed for a fully configured environment.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.