4

While I was trying to execute the mkvirtualenv command on the command prompt, I was getting this error:

C:\Users\mukesh>mkvirtualenv myproject 
'mkvirtualenv' is not recognized as an internal or external command, operable program or batch file.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    Possible duplicate of [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/questions/41454769/what-is-the-reason-for-x-is-not-recognized-as-an-internal-or-external-command) – aschipfl Jun 26 '19 at 17:33

5 Answers5

18

For Python 3.3 or newer, Commands for installing, creating and activate virtual environment has been changed.

You can install virtual environment using pip:

py -m pip install --user virtualenv

For creating new environment:

py -m venv myproject

To activate your virtual environment:

.\myproject\Scripts\activate

After activating virtual environment, You’ll see “(myproject)” next to the command prompt.

Rajan Tejani
  • 189
  • 1
  • 3
  • But what is the spirit of using additional switch i.e. --user?Is not it making complex the older simpler one. – Learner Jul 08 '20 at 12:01
  • Thank you, this worked! The other answer that is directly from django docs did not work for me. https://docs.djangoproject.com/en/2.2/howto/windows/ – Azhar Ansari Nov 23 '21 at 05:07
9

You may find this link useful, as it shows the steps required. It is possible you have simply missed the earlier steps, leading to the error.

The below information is from: https://docs.djangoproject.com/en/2.2/howto/windows/

This will run you through the creation of a virtual environment on Windows:

Install virtualenv and virtualenvwrapper¶

virtualenv and virtualenvwrapper provide a dedicated environment for each Django project you create. While not mandatory, this is considered a best practice and will save you time in the future when you’re ready to deploy your project. Simply type:

pip install virtualenvwrapper-win

Then create a virtual environment for your project:

mkvirtualenv myproject

The virtual environment will be activated automatically, and you’ll see “(myproject)” next to the command prompt to designate that. If you start a new command prompt, you’ll need to activate the environment again using:

workon myproject
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Compoot
  • 2,227
  • 6
  • 31
  • 63
1

To create a virtual environment,

decide upon a directory where you want to place it, and run the venv module as a script with the directory path:

python3 -m venv tutorial-env

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter and various supporting files.

A common directory location for a virtual environment is .venv. This name keeps the directory typically hidden in your shell and thus out of the way while giving it a name that explains why the directory exists. It also prevents clashing with .env environment variable definition files that some tooling supports.

Once you’ve created a virtual environment, you may activate it.

On Windows, run:

tutorial-env\Scripts\activate.bat

On Unix or MacOS, run:

source tutorial-env/bin/activate

1

some times the environment will not active because users are not allowed to run scripts in the current system so you should

  1. -go to windows PowerShell
  2. -run as administrator
  3. -then past this code

-Set-ExecutionPolicy -Scope CurrentUser Unrestricted

now you are allowed to run scripts on your system then try this again

py -m pip install --user virtualenv

For creating new environment:

py -m venv myproject

To activate your virtual environment:

.\myproject\Scripts\activat
kazi Sakin
  • 11
  • 2
0

If you have a Windows computer (and installed the Windows version virtualenvwrapper-win), make sure you add the Scripts folder to the path. As per the installation instructions:

To use these scripts from any directory, make sure the Scripts subdirectory of Python is in your PATH. For example, if python is installed in C:\Python27, you should make sure C:\Python27\Scripts is in your PATH.

Here's some decent instructions on how to edit your path. Nowadays you can probably create a new entry after selecting to edit the path environment variable. That new entry should just be the location of the Scripts folder (including the Scripts folder). No need to add semicolons to a super long path name - it generally does that for you nowadays. You'll probably have to restart your computer for it to take effect.

You can find out where your Python is installed here. If you are on Windows and installed Python via the Microsoft Store, you won't see a Scripts folder. In that case, install Python from the Python website, not from the Microsoft Store.

Pro Q
  • 4,391
  • 4
  • 43
  • 92