Sanyukta Suman Logo Sanyukta Suman
Blog Post Thumbnail
Image credit: Source

Working with Virtual Environment

Published on Sept 5, 2025

Encountering errors when running a Python script is a common experience, but most issues can be resolved with a systematic approach. This guide will walk you through the most frequent problems, from environment setup to dependency conflicts, using a real-world debugging example.

Step 1: "I can't run my Python file"

This is the most common issue, and it almost always comes down to two things: your current location in the terminal or your virtual environment.

Problem: FileNotFoundError or No such file or directory

Reason: You are not in the correct folder where your script is saved. The terminal needs to be pointed to the exact location of your file.

How to Fix:

  1. Check your current location: In your terminal, type pwd (print working directory) and press Enter. This will show you the full path of the folder you are in.
  2. Verify your files: Type ls (list files) and press Enter. This will show you all the files in the current folder. Make sure your Python file's name (your_script.py) is visible.
  3. Navigate to the correct folder: If your file is not there, use the cd (change directory) command to move to the right location. For example, cd ~/Documents/my_project.

Problem: ModuleNotFoundError: No module named '...'

Reason: Your virtual environment is not activated. This is a critical step because the packages your script needs (like pandas, shap, or numpy) are installed inside a special isolated environment. If you don't activate it, your system's default Python installation won't be able to find them.

How to Fix:

  1. Check your terminal prompt: Look for (venv) at the beginning of the line. If it's not there, your environment is inactive.
  2. Activate your virtual environment: Run the activation script.

    source venv/bin/activate
    

    Your prompt should change to confirm the environment is active.

  3. Run your script: Now, use the generic python command, which automatically uses the interpreter from your venv.

    python your_script_name.py
    

Step 2: "My code executed but I got a dependency error"

Sometimes, a script will start to run but then crash with an error like ValueError: Please install the 'db-dtypes' package. This happens when a specific, required package is missing.

Problem: ModuleNotFoundError or ValueError: Please install the '...' package

Reason: A dependency required by your script, or one of its installed packages, is not present in your virtual environment. This can happen if you forgot to add it to your requirements.txt file before installing.

How to Fix:

  1. Install the missing package: Make sure your venv is active, then use pip to install the package mentioned in the error message.

    pip install db-dtypes
    
  2. Run the script again. Once the installation is complete, the error should be resolved.

Production Best Practices: Locking Dependencies

For production environments, you can't rely on pip to choose a version. You need to lock down exact versions to ensure consistency.

How to Fix:

  1. After successfully installing all packages and confirming your script works locally, use pip freeze to generate a new requirements.txt file with locked versions.

    pip freeze > requirements.txt
    

    This command will overwrite your file with a list like db-dtypes==1.2.0, guaranteeing that your production environment uses the exact same versions as your local one.

Step 3: "I'm getting a urllib3 warning about LibreSSL"

This is a non-critical warning, not an error. It does not stop your script from running, but it can be distracting.

Problem: WARNING: The script google-oauthlib-tool is installed... which is not on PATH.

Reason: A script installed by one of your packages is in a directory that is not included in your system's PATH. The PATH is a list of directories your shell checks for executable programs.

How to Fix (Recommended):

  1. Add the directory to your PATH: Open your shell's configuration file (e.g., .zshrc on macOS) and add the directory to your PATH.

    nano ~/.zshrc
    
  2. Consolidate your PATH exports: To avoid overwriting issues, it's best to have a single export PATH command that includes all your necessary directories.

    export PATH="/Users/sanyuktasuman/Library/Python/3.9/bin:$PATH"
    
  3. Save the file and reload your shell configuration with source ~/.zshrc to apply the changes.

By following these steps, you can systematically diagnose and fix the most common Python script errors, turning what seems like a daunting problem into a straightforward solution.