Opening Python from within linux using Windows Terminal command line arguments causes it to use the Windows installation of python instead of linux's #17288

Closed
opened 2026-01-31 05:37:56 +00:00 by claunia · 5 comments
Owner

Originally created by @DiscantX on GitHub (Apr 20, 2022).

Windows Terminal version

1.12.10982.0

Windows build number

10.0.19044.1645

Other Software

python27
python3.10
Ubuntu 20.04.2 LTS

Steps to reproduce

From Ubuntu in Windows Terminal, run the command:

cmd.exe /c "wt.exe" -p "Ubuntu" python pythonpath.py

pythonpath.py

from time import sleep
import sys
print(sys.path)
sleep(60)

Now run the script on its own:

python pythonpath.py

Command line arguments for doing this were found in the Microsoft documentation.

Expected Behavior

When a python script is run from within a linux subsystem, it uses the subsystem's python installation.

image

Actual Behavior

When the same script is run using Windows Terminal command line arguments, it is run using the the windows installation of python, despite correctly opening in a linux terminal.

image

Originally created by @DiscantX on GitHub (Apr 20, 2022). ### Windows Terminal version 1.12.10982.0 ### Windows build number 10.0.19044.1645 ### Other Software python27 python3.10 Ubuntu 20.04.2 LTS ### Steps to reproduce From Ubuntu in Windows Terminal, run the command: `cmd.exe /c "wt.exe" -p "Ubuntu" python pythonpath.py` ### pythonpath.py `from time import sleep` `import sys` `print(sys.path)` `sleep(60)` Now run the script on its own: `python pythonpath.py` Command line arguments for doing this were found in the[ Microsoft documentation](https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=linux). ### Expected Behavior When a python script is run from within a linux subsystem, it uses the subsystem's python installation. ![image](https://user-images.githubusercontent.com/22719102/164172235-21387738-56fd-4840-99b5-bcbea7b7dad3.png) ### Actual Behavior When the same script is run using Windows Terminal command line arguments, it is run using the the windows installation of python, despite correctly opening in a linux terminal. ![image](https://user-images.githubusercontent.com/22719102/164171700-fdec9915-c5e0-44f9-892b-928635ef07fc.png)
claunia added the Issue-QuestionNeeds-TriageIssue-BugNeeds-Tag-FixNeeds-Attention labels 2026-01-31 05:37:56 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Apr 20, 2022):

Ahh okay I see what's happening here.

wt -p "whatever" {foo} replaces the commandline of your "whatever" profile and instead runs {foo}. It doesn't append foo to the commandline of the profile. It doesn't launch the profile, then run the commandline.

You might be able to workaround this for now with

wt.exe -p "Ubuntu" -- wsl -d Ubuntu python pythonpath.py

See also #5528

@zadjii-msft commented on GitHub (Apr 20, 2022): Ahh okay I see what's happening here. `wt -p "whatever" {foo}` _replaces_ the commandline of your "whatever" profile and instead runs `{foo}`. It doesn't _append_ `foo` to the commandline of the profile. It doesn't launch the profile, then run the commandline. You might be able to workaround this for now with ``` wt.exe -p "Ubuntu" -- wsl -d Ubuntu python pythonpath.py ``` See also #5528
Author
Owner

@DiscantX commented on GitHub (Apr 21, 2022):

So this kind of works but not really. I ran into a couple of problems.

  1. I'd like to keep my workflow within bash without switching to cmd prompt.

  2. It doesn't seem to play nicely with pyenv shims (at least I think that was the cause). From within bash if I type "Python" it opens Python 3.10.4. Using the command you provided, it cannot find Python as a command at all. But if I use Python3 instead, it opens Python 3.8.10 (my Ubuntu system python version). So it still isn't using my linux environment the way I have it set up.

image
image

  1. It doesn't work for my specific use case (which to be fair I didn't mention before because it is admittedly a bit convoluted):

What I'm actually trying to do is use Python subprocesses via Popen to launch multiple python scripts within Windows Terminal (using WT's tabbing and panes to organize the running processes)...

Now, after some fiddling I got this to work, except that some of these scripts also use subprocesses to run linux-specific commands such as grep, which are not recognized as commands, since the wt commandline I used to launch the script is actually using Windows, which of course won't recognize the linux commands.

@DiscantX commented on GitHub (Apr 21, 2022): So this _kind_ of works but not really. I ran into a couple of problems. 1. I'd like to keep my workflow within bash without switching to cmd prompt. 2. It doesn't seem to play nicely with pyenv shims (at least I think that was the cause). From within bash if I type "Python" it opens Python 3.10.4. Using the command you provided, it cannot find Python as a command at all. But if I use Python3 instead, it opens Python 3.8.10 (my Ubuntu system python version). So it still isn't using my linux environment the way I have it set up. ![image](https://user-images.githubusercontent.com/22719102/164344105-1deb3a9b-229b-408c-9c13-2724073961a4.png) ![image](https://user-images.githubusercontent.com/22719102/164345764-dc3ecb87-9dfb-400e-a4be-874ade55bcb2.png) 3. It doesn't work for my specific use case (which to be fair I didn't mention before because it is admittedly a bit convoluted): What I'm actually trying to do is use Python subprocesses via Popen to launch multiple python scripts within Windows Terminal (using WT's tabbing and panes to organize the running processes)... Now, after some fiddling I got this to work, _except_ that some of these scripts also use subprocesses to run linux-specific commands such as grep, which are not recognized as commands, since the wt commandline I used to launch the script is actually using Windows, which of course won't recognize the linux commands.
Author
Owner

@zadjii-msft commented on GitHub (Aug 24, 2022):

Hey, sorry I lost track of this.

It doesn't seem to play nicely with pyenv shims

I bet that has something to do with launching the application directly, rather than via a login shell. Methinks that Python 3.10.4 is probably installed only for the default user's path, but the Python 3.8.10 version is available globally as python3. That's conjecture, and I don't totally know how paths are resolved in this case.

Sounds like you sorta figured this out, sorta didn't. Did you think #5528 would help with the last bits of your use case here/?

@zadjii-msft commented on GitHub (Aug 24, 2022): Hey, sorry I lost track of this. > It doesn't seem to play nicely with pyenv shims I bet that has something to do with launching the application directly, rather than via a login shell. Methinks that Python 3.10.4 is probably installed only for the default user's path, but the Python 3.8.10 version is available globally as python3. That's conjecture, and I don't totally know how paths are resolved in this case. Sounds like you sorta figured this out, sorta didn't. Did you think #5528 would help with the last bits of your use case here/?
Author
Owner

@DiscantX commented on GitHub (Aug 25, 2022):

So it's been a while since I looked at this, but it seems that your solution did solve the inability to append arguments to the command, but the root problem for me ended up being that WSL still seems to be using the Windows version of Python on some level since it is being called from command prompt. This might be a related but different issue than the one I was originally facing, I'm not sure.

I'm just going to post the code I was working with, in case that helps:

Screenshots:
auto-1
auto-2
auto-3
working
grep-sub

Code files (can't post .py files):
auto.txt
working.txt
grep-sub.txt

This was obviously a work in progress but it should get the idea across. If you try to run grep-sub.py as a subprocess in this script (from Ubuntu in WT), it doesn't recognize grep, but if you run it directly from the shell, it does. This leads me to think the subprocess is being run under the Windows environment rather than the Linux one.

EDIT: Sorry, the reference to "test.py" on line 5 of grep-sub.py should be "working.py"

@DiscantX commented on GitHub (Aug 25, 2022): So it's been a while since I looked at this, but it seems that your solution did solve the inability to append arguments to the command, but the root problem for me ended up being that WSL still seems to be using the Windows version of Python on some level since it is being called from command prompt. This might be a related but different issue than the one I was originally facing, I'm not sure. I'm just going to post the code I was working with, in case that helps: Screenshots: ![auto-1](https://user-images.githubusercontent.com/22719102/186567325-d382cb1b-c9f1-4f5a-b01b-bc37659fd7a6.png) ![auto-2](https://user-images.githubusercontent.com/22719102/186567336-72798cfd-623a-492e-bb98-272032bac1a4.png) ![auto-3](https://user-images.githubusercontent.com/22719102/186567348-5d8dcaf5-65cc-44c2-badd-16932f040281.png) ![working](https://user-images.githubusercontent.com/22719102/186567355-84d68d69-3254-44f0-a6f4-1d70b58218f3.png) ![grep-sub](https://user-images.githubusercontent.com/22719102/186567367-db108803-55fe-4ec1-9b7d-21d3efea5a85.png) Code files (can't post .py files): [auto.txt](https://github.com/microsoft/terminal/files/9421050/auto.txt) [working.txt](https://github.com/microsoft/terminal/files/9421051/working.txt) [grep-sub.txt](https://github.com/microsoft/terminal/files/9421053/grep-sub.txt) This was obviously a work in progress but it should get the idea across. If you try to run grep-sub.py as a subprocess in this script (from Ubuntu in WT), it doesn't recognize grep, but if you run it directly from the shell, it does. This leads me to think the subprocess is being run under the Windows environment rather than the Linux one. **EDIT:** Sorry, the reference to "test.py" on line 5 of grep-sub.py should be "working.py"
Author
Owner

@zadjii-msft commented on GitHub (Aug 25, 2022):

Yea, that looks like you're just passing wt -w 0 -p Ubuntu python .... and yea, that's gonna use the python.exe on the Windows path. You'd need to do something akin to wt -w 0 -p Ubuntu wsl -d Ubuntu bash -c python .... to get it to use the wsl one (of course, I'm making up the bash -c bit, I can't recall the right args off the top of my head.)

This definitely seems like another use case for #5528. I'm gonna call this a "dupe", but actually move this to a discussion thread. Thanks!

@zadjii-msft commented on GitHub (Aug 25, 2022): Yea, that looks like you're just passing `wt -w 0 -p Ubuntu python ....` and yea, that's gonna use the `python.exe` on the Windows path. You'd need to do something akin to `wt -w 0 -p Ubuntu wsl -d Ubuntu bash -c python ....` to get it to use the wsl one (of course, I'm making up the `bash -c` bit, I can't recall the right args off the top of my head.) This definitely seems like another use case for #5528. I'm gonna call this a "dupe", but actually move this to a discussion thread. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#17288