Introduction
Encountering the error “django-admin is not recognized as an internal or external command” is common when working with Django in a virtual environment. This guide provides a step-by-step solution to resolve this issue and ensure that django-admin
works correctly inside a virtual environment.
Step 1: Verify Python Installation
Ensure Python is installed by running the following command in the terminal or command prompt:
1 |
python --version |
If Python is not installed, download and install it from the official Python website.
Step 2: Create a Virtual Environment
Navigate to your project directory and create a virtual environment using the following command:
1 |
python -m venv venv |
Here, venv
is the name of the virtual environment. You can replace it with any preferred name.
Step 3: Activate the Virtual Environment
Activate the virtual environment using the appropriate command for your operating system:
Windows:
1 |
venv\Scripts\activate |
macOS/Linux:
1 |
source venv/bin/activate |
Once activated, your command prompt or terminal should show (venv)
at the beginning, indicating that the virtual environment is active.
Step 4: Install Django
With the virtual environment activated, install Django using pip:
1 |
pip install django |
To verify the installation, run:
1 |
django-admin --version |
If Django is correctly installed, this command should return the Django version.
Step 5: Ensure django-admin is Available
If running django-admin
still gives an error, check if the Scripts directory is in your system’s PATH.
Windows:
Manually add the Scripts directory to your system’s PATH:
- Open Command Prompt and type:
1 |
set PATH=%PATH%;C:\path\to\your\venv\Scripts |
- Replace
C:\path\to\your\venv
with the actual path to your virtual environment.
- Restart your command prompt and try running
django-admin
again.
macOS/Linux:
Add the virtual environment’s bin
directory to the PATH:
1 |
export PATH=$PATH:/path/to/your/venv/bin |
Replace /path/to/your/venv
with the actual path to your virtual environment.
Step 6: Create a Django Project
Now, you should be able to create a Django project without errors:
1 |
django-admin startproject myproject |