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
Understand Replit's automatic virtual environment
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.
1# Run these in the Shell to inspect your environment2which python3python --version4pip listExpected 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.
Install packages with pip
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.
1# Install a single package2pip install requests34# Install from a requirements file5pip install -r requirements.txt67# Create a requirements file from current environment8pip freeze > requirements.txt910# Install a specific version11pip install flask==3.0.0Expected result: Packages install into the project's virtual environment. Running pip list shows the newly installed packages.
Use poetry for dependency management
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.
1# Install poetry if not already available2pip install poetry34# Initialize a new poetry project5poetry init --no-interaction67# Add a package8poetry add requests910# Add a dev-only package11poetry add --group dev pytest1213# Install all dependencies from lock file14poetry install1516# Run a script through poetry's environment17poetry run python main.pyExpected result: Poetry creates pyproject.toml and poetry.lock files. Packages are installed and available for import.
Change the Python version via Nix
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.
1# replit.nix — change the Python version here2{ pkgs }: {3 deps = [4 pkgs.python3115 pkgs.python311Packages.pip6 ];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.
Handle system-level dependencies with Nix
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.
1# replit.nix with system dependencies for common packages2{ pkgs }: {3 deps = [4 pkgs.python3115 pkgs.python311Packages.pip6 pkgs.zlib7 pkgs.libjpeg8 pkgs.libpng9 pkgs.postgresql10 ];11}Expected result: After reloading the Shell with exit, pip install commands for packages like Pillow and psycopg2 succeed without 'failed building wheel' errors.
Troubleshoot common environment errors
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.
1# Diagnostic commands for environment issues2which python3python --version4pip list5pip check # verify no broken dependencies67# Nuclear reset: delete and recreate the environment8rm -rf .pythonlibs9pip install -r requirements.txtExpected 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
1#!/bin/bash2# Python environment setup script for Replit3# Run this in the Shell after creating a new Python project45echo "=== Python Environment Setup ==="6echo ""78# Check current Python version9echo "Python version:"10python --version11echo ""1213# Check pip14echo "pip version:"15pip --version16echo ""1718# Check virtual environment19echo "Python path:"20which python21echo ""2223# Install from requirements if the file exists24if [ -f "requirements.txt" ]; then25 echo "Installing from requirements.txt..."26 pip install -r requirements.txt27else28 echo "No requirements.txt found. Creating one..."29 echo "# Add your project dependencies here" > requirements.txt30 echo "# Example:" >> requirements.txt31 echo "# requests==2.31.0" >> requirements.txt32 echo "# flask==3.0.0" >> requirements.txt33 echo "Created requirements.txt template."34fi3536echo ""3738# Verify no broken dependencies39echo "Checking for dependency conflicts..."40pip check4142echo ""43echo "=== Environment Ready ==="44echo "Installed packages:"45pip list --format=columnsCommon 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.
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?
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.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation