9

GOAL:

I am trying to create a new virtual environment with a specific python version.

ISSUE

When I type:

python3 -m virtualenv --python="D:\Python_Versions\python.exe" new_virtualenv

I get:

RuntimeError: failed to find interpreter for Builtin discover of python_spec='D:\Python_Versions\python.exe'

WHAT I HAVE DONE:

  1. Install Ubuntu Bash for Windows:

Open Windows Command Prompt

wsl --install

Source: https://learn.microsoft.com/en-us/windows/wsl/install

  1. Install Pip

Open Windows command prompt:

bash

Then

 sudo apt-get install python-pip
  1. Install Virtualenv

Type

pip install virtualenv
  1. Download python version (Here 3.6)

Go to: https://www.python.org/downloads/release/python-360/

Download: Windows x86-64 executable installer

Install into folder of your choice, i.e. D:\Python_Versions\

  1. Attempt to create virtual environment with specific python version (Failed)

In the command prompt, go to your project folder ("cd ...") and type:

python3 -m virtualenv --python="D:\Python_Versions\python.exe" new_virtualenv

Source: Use different Python version with virtualenv

MattDMo
  • 100,794
  • 21
  • 241
  • 231
james
  • 195
  • 1
  • 1
  • 9
  • 1
    given how common virtual environments are and how much they are recommended, it should be much simpler to create on. – D.L Apr 02 '22 at 15:27
  • @D.L I agree, but this was the simplest way I found, as I don't want to use anaconda, as I want to have a clean install of python and only pip. No added obscure layer. – james Apr 02 '22 at 15:29
  • There may be a problem calling in linux an windows executable. Try download and install python for linux in the bash. More on install for ubuntu: https://www.rosehosting.com/blog/how-to-create-a-python-virtual-environment-on-ubuntu-20-04/ – Radek Rojík Apr 04 '22 at 17:00
  • Install Python distutils: https://stackoverflow.com/a/72874000/1925257 – xyres Nov 09 '22 at 04:52

1 Answers1

6

In general, your problem isn't in trying to specify a different Python version, but in trying to use a Python executable for a different operating system. When in WSL, you are essentially running Linux binaries and libraries, but you are attempting to have the Linux Python create a virtual environment using the Windows Python executable.

There are a number of reasons why this is a bad idea, and the error you are getting demonstrates one of them:

  • The Linux Python interpreter doesn't understand Windows paths, and vice-versa.

In other words, python3, the Linux Python interpreter, can't parse the path D:\Python_Versions\python.exe.

WSL provides a nice "translation" utility wslpath that can convert between Linux paths and Windows paths. For instance:

  • /mnt/d/Python_Versions/python.exe <-> D:\Python_Versions\python.exe
  • /home/user/username/src/python_project <-> \\wsl$\<distroname>\home\user\username\src\python_project

However, that's not going to help you with this particular problem. You would need the Linux Python to convert all paths like this, which it isn't going to do.

In addition, while WSL can run Windows executables (such as python.exe), those executables won't be able to access Linux libraries and processes.

And yet another reason this won't work -- Windows Python can't operate on the pseudo-network share drive that otherwise allows Windows processes to access WSL/Linux files. For instance, if you are in PowerShell:

cd $env:USERPROFILE\Documents
mkdir src\python
cd src\python
python -m venv new_virtualenv
# Works

But (also from PowerShell):

cd \\$wsl\<distroname>\home\<username>\
mkdir src\python
cd src\python
python -m venv new_virtualenv
# Error: [WinError 1] Incorrect function

Assuming that Windows Python is in your Windows path, you can, on the other hand, in WSL:

cd /mnt/c/Users/<username>/Documents
mkdir src/python
cd src/python
python.exe -m venv new_virtualenv

However, you can't activate the virtual environment since Windows Python doesn't create activation scripts for Linux. It does create a Bash script, but it's for Git Bash, and if you examine it (./Scripts/activate in that venv directory), you'll see it still uses Windows paths.

There's really just no way around it:

  • Use the Linux Python binaries and modules when developing in Linux.
  • Use the Windows Python binaries and modules when developing in Windows.

Conversely:

  • When in a Windows environment, use the Windows Python binaries and modules.
  • When in a Linux/WSL environment, use the Linux Python binaries and modules.

Side note: As you might realize by now, the --python flag for virtualenv is designed to allow you to specify a different Python version that matches the OS architecture. In other words, if you are on Ubuntu/WSL (which I don't believe you are, given your pip example):

sudo apt install python2
sudo apt install python3-pip
pip install virtualenv
python -m virtualenv --python=/usr/bin/python2 new_virtualenv
cd new_virtualenv
source bin/activate
python --version
# Will return Python 2.7.18

Update, addressing your additional comments:

Do you have any suggestions of how to cleanly install Python on Windows with different environments that use different Python versions (and preferably all access one Spyder installation), while NOT reverting back to something like conda or that sort?

Since Spyder is a Python app itself, it's going to have the same problem communicating "cross platform" that the Python virtualenv does. You can try to set your Windows Python executable as the WSL Spyder terminal, but it wouldn't actually be able to launch it since the Linux Spyder won't be able to communicate with the spyder-kernel module even if you install it in Windows.

And vice-versa.

I'm just not aware of (and couldn't find any, in my research) WSL <-> Windows interop features in Spyder.

You haven't explained your use-case for needing both Windows and Linux Python environments, but if you are going to use Spyder, and you really need both Windows and Linux Python binaries, then I'm fairly confident that you'll need to install Spyder twice -- Once in Windows and once in WSL/Linux.

And from what I understand, Conda is the best option for installing Spyder in Linux. You might also check out this Ask Ubuntu answer, but unfortunately that did not work for me.

Another option

If you aren't completely tied to Spyder, you might consider Visual Studio Code (the Windows version). It does have the ability to interact with both Windows and Linux/WSL Python interpreters.

After installing the "Remote - WSL" extension (from Microsoft), it can interact with the Linux Python by setting up a Linux server inside WSL. You can access Windows Python virtual environments (while in "Windows mode") and WSL virtual environments (while in "Remote - WSL" mode).

While I haven't tried these particular features myself, I am fairly certain, based on what I know of VSCode and WSL, that you can:

  • Set up a Windows virtualenv with multiple Windows Python versions.
  • Set up a Linux virtualenv with multiple Linux Python versions.

It has debugging support for both platforms, can support variable inspection (based on this answer), and probably much, much more through its rich extension library (currently 652 extensions returned from a search for "Python").

It's definitely worth checking out to see if it will meet your needs since it's one of the few Windows IDE's that can interoperate with WSL directly. My understanding is that Pycharm also has WSL integration, but I haven't explored that personally.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • Many thanks for your explanation of why I am facing this error! Its very clear now! Now, the obvious question is: What do I do about it ? Do you have any suggestions of how to cleanly install python on windows with different environments that use different python version (and preferably all access one spyder installation), while NOT reverting back to something like conda or that sort ? – james Apr 05 '22 at 16:59
  • @james Updated information on that aspect at the bottom of the answer. – NotTheDr01ds Apr 05 '22 at 20:46
  • @james Not really looking for the bounty at this point (I believe I already have it), but I am curious if the additional information I provided answered your question? I did install Conda and Spyder in researching your answer, I can see why you don't want to go the Conda route. It's pretty "invasive" (IMHO). On the bright side, you can always create multiple WSL instances - One for Conda/Spyder and another without. That's one of the great things about WSL. – NotTheDr01ds Apr 12 '22 at 01:58