Last updated: Apr 4, 2024
Reading timeยท4 min
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.
Open your shell in your project's root directory and run the following command to activate your virtual environment.
# ๐๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1
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.
You can use the deactivate
command if you need to deactivate the virtual
environment.
# ๐๏ธ 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.
# ๐๏ธ works for both CMD (Command Prompt) and PowerShell venv\Scripts\activate
And here is the activation command for macOS and Linux.
# ๐๏ธ activate on Linux or MacOS source venv/bin/activate
If you haven't created a virtual environment, make sure to create one first.
# ๐๏ธ 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.
python -m venv venv # ๐๏ธ activate when using Git Bash source venv/Scripts/activate # ๐๏ธ install modules in the virtual environment pip install requests
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.
To add Python to your user's PATH environment variable:
python -c "import os, sys; print(os.path.dirname(sys.executable))" where python
For me, the path is the following.
C:\Users\YOUR_USER\AppData\Local\Programs\Python\Python310
Note that I have Python 3.10 installed, which is reflected in the PATH.
Scripts
directory that
is located in your Python3X folder. This is where the executable files are
located.For me, it is the following path.
C:\Users\YOUR_USER\AppData\Local\Programs\Python\Python310\Scripts
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.
Download the installer from the official python.org website.
If you have Python already installed, start the installer and click on "Modify".
You can leave the optional features ticked.
If that didn't work, your Python installation might be corrupted.
Start the installer again and click on "Uninstall".
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.
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.
You can learn more about the related topics by checking out the following tutorials: