4

I am trying to create a new python 3.7 virtual environment on my local computer running Windows 8. I have python versions 3.6, 3.7, and 3.8 installed. Their exe's are named python36, python37, and python, respectively. All three are correctly added to PATH because I can enter each interpreter.

Within my new project's directory I tried to create a virtual environment with python37 -m venv env. It produced an error: Error: [WinError 2] The system cannot find the file specified, but it still created the directory. However the Scripts subfolder is empty except for pythonw.exe.

In this question someone suggests doing python37 -m venv env --without-pip. When I tried this, the activation/deactivation scripts were created, but the virtual environment is using python 3.8.

It is my understanding that venv will create the virtual environment with what ever python exe you use to call it, so I don't understand how this can happen. I've verified that python37 points to the correct place with where python37, and can even enter the 3.7 interactive interpreter.

Scofflaw
  • 152
  • 2
  • 10
  • Does this answer your question? [Python venv env Fails - \[WinError 2\] The system cannot find the file specified](https://stackoverflow.com/questions/61669873/python-venv-env-fails-winerror-2-the-system-cannot-find-the-file-specified) – J. Doe Feb 13 '22 at 19:08

2 Answers2

1

The problem was that I renamed the python exe's. I don't know exactly what goes wrong, but presumably at some point venv tries to find python.exe and is thrown off by the name.

Changing them back to python.exe and differentiating between the versions with their location fixed the problem.

Edit: Check out Eryk's comments for more details.

Scofflaw
  • 152
  • 2
  • 10
  • The base "python.exe" must exist in order for the "python.exe" launcher to be copied over from "\Lib\venv\scripts\nt". Unfortunately it's hard coded. Even if you keep the original "python.exe" and use a hardlink or symlink (e.g. "python37.exe" -> "python.exe"), the base name of `sys.executable` will be "python37.exe", which causes the installation of pip to fail, since it tries to run "python37.exe" in the environment, which doesn't exist. In principle, it would work with a symlink if Python resolved `sys.executable` in Windows like it does in Unix. I consider this a bug. – Eryk Sun Jan 25 '20 at 14:26
  • As a workaround, you can create a "python37.lnk" file (shell shortcut) beside "python.exe" in the installation directory that runs it with the fully-qualified path. Clear the "start in" field to leave it empty in order to inherit the working directory of the caller that's running the shell link. Add ".LNK" to `PATHEXT` in the system environment variables, and you'll be able to run `python37` from the command line in CMD or PowerShell. (Maybe use the name "python3.7.lnk", however, which is the normal name for this in Unix systems.) – Eryk Sun Jan 25 '20 at 14:32
  • Note that if you're using Explorer to create the shell link, by default it hides the .lnk extension, so make sure to just name it "python37" or "python3.7" without the extension. If Python is installed for all users in a system directory, create the shell link on your desktop, and then paste it into the installation directory. Explorer should prompt you to get administrator access. – Eryk Sun Jan 25 '20 at 14:41
-1

First create folder at any drive then go to that folder and install virtualenv package using pip.

pip install virtualenv

Then create your virtual environment.

mkvirtualenv myvirtualenv

Then use below command to activate virtualenv in windows.

myvirtualenv\Scripts\activate

After this you can install related package in current virtual environment.

The Python Standard Library for Creating Virtual Environment

Saket Yadav
  • 967
  • 1
  • 10
  • 23