Sling Academy
Home/Python/How to Use Code Runner in Python Virtual Environments

How to Use Code Runner in Python Virtual Environments

Last updated: December 01, 2022

If you’re using VS Code (Visual Studio Code), then you can run a Python code file by performing the following command in your terminal window:

python3 my_file.py

However, it is possible to execute a code file just by hitting a button with your mouse by using an extension name Code Runner. After installing the extension, you will see a play button with a triangle shape in the top right corner of your window:

However, if you’re trying to execute a Python script in a virtual environment (venv, conda, pipenv, etc) with Code Runner, you will run into an error. The error messages can vary but are usually the following:

ModuleNotFoundError: No module named 'example'

Or:

/bin/sh: python: command not found

The reason is that Code Runner runs before VS Code initializes the virtual environment. Don’t worry. We have a working solution for this problem, which isn’t hard to follow.

The Solution

This solution solves the issue on both Windows and Mac.

1. In the root directory of your project, create a new folder named .vscode, and inside this folder, add a new file name settings.json. Copy the following and paste it into your settings.json:

{
    "code-runner.executorMap": {
        "python": "$pythonPath $fullFileName"
    }
}

Screenshot:

2. Reload (or restart) your VS Code, then try to execute your Python script with Code Runner, and it should work as expected. You will see the results in the output window, like this:

If you later work with another Python project in a virtual environment, copy the entire .vscode folder into the root directory of that project. Everything remains the same and no further changes need to be made.

I hope this helps. Happy coding!

Next Article: Python: How to rename a virtual environment directory

Previous Article: Python virtual environment: 3 Ways to upgrade all packages

Series: Basic Python Tutorials

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots