source is not recognized as an internal or external command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# source is not recognized as an internal or external command

The error "'source' is not recognized as an internal or external command, operable program or batch file" occurs when we use the source command to activate a virtual environment on Windows.

To solve the error, use the venv\Scripts\activate command instead.

source is not recognized as internal or external command

Open your shell in your project's root directory and run the following command to activate your virtual environment.

cmd
# ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1

activate virtual environment cmd

Note: the command assumes that you named your virtual environment venv. If you name it something else, make sure to update the start of the command.

Make sure to use the correct command for CMD and PowerShell.

activate virtual environment powershell

You can use the deactivate command if you need to deactivate the virtual environment.

cmd
# ๐Ÿ‘‡๏ธ deactivate virtual environment deactivate # ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1

Alternatively, you can use the universal venv\Scripts\activate command which works regardless if you use CMD or PowerShell.

cmd
# ๐Ÿ‘‡๏ธ works for both CMD (Command Prompt) and PowerShell venv\Scripts\activate

And here is the activation command for macOS and Linux.

shell
# ๐Ÿ‘‡๏ธ activate on Linux or MacOS source venv/bin/activate

activate virtual environment macos linux

If you haven't created a virtual environment, make sure to create one first.

shell
# ๐Ÿ‘‡๏ธ use the correct version of Python when creating VENV python -m venv venv # ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ Activate on Unix or MacOS source venv/bin/activate # ๐Ÿ‘‡๏ธ install modules in the virtual environment pip install requests

If the python -m venv venv command doesn't work, try the following 2 commands:

  • py -m venv venv
  • python3 -m venv venv

Your virtual environment will use the version of Python that was used to create it.

If you use Git Bash, you would have to use the source venv/Scripts/activate command.

GitBash
python -m venv venv # ๐Ÿ‘‡๏ธ activate when using Git Bash source venv/Scripts/activate # ๐Ÿ‘‡๏ธ install modules in the virtual environment pip install requests

activate virtual environment git bash

If you still get an error when activating or creating your virtual environment, you have to add Python to your user's PATH environment variable.

# Add Python to your user's PATH environment variable

To add Python to your user's PATH environment variable:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

click environment variables

  1. In the "User variables for YOUR_USER" section, select the "Path" variable and click "Edit".

select user path click edit

  1. Click on "New" and then click "Browse".

click new browse

  1. You can use one of the following commands to check where your Python installation is located.
cmd
python -c "import os, sys; print(os.path.dirname(sys.executable))" where python

find python path

For me, the path is the following.

cmd
C:\Users\YOUR_USER\AppData\Local\Programs\Python\Python310

Note that I have Python 3.10 installed, which is reflected in the PATH.

  1. Add the path to Python and then add the path to the Scripts directory that is located in your Python3X folder. This is where the executable files are located.

For me, it is the following path.

cmd
C:\Users\YOUR_USER\AppData\Local\Programs\Python\Python310\Scripts

add scripts folder

  1. Once the two paths are added, confirm the changes by clicking on the "OK" button twice.

added python and pip to path

  1. Close your Command prompt application and then reopen it.
Note that you must restart your Command prompt shell for the changes to take effect.

You might also have to restart your PC, but that's not always necessary.

If you still encounter issues, try to add Python to your PATH using the official installer.

  1. Download the installer from the official python.org website.

  2. If you have Python already installed, start the installer and click on "Modify".

click modify

You can leave the optional features ticked.

optional features

  1. On the "Advanced Options" screen, make sure to tick the "Add Python to environment variables" option.

add python to environment variables

  1. Once the "Add Python to environment variables" checkbox is checked, click "Install".
Now your Python installation should be set up correctly and Python should be added to your user's PATH environment variable.

If that didn't work, your Python installation might be corrupted.

Start the installer again and click on "Uninstall".

uninstall python

Now that you don't have Python installed on your machine, start the installer again and make sure to tick the "Add python.exe to PATH" option.

add python exe to path

The checkbox won't be checked by default.

Once the "Add python.exe to PATH" checkbox is checked, click on "Install Now".

After the installation, Python will be installed and configured properly.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev