Home > Blockchain >  Can't run any Python files on Ubuntu(WSL)
Can't run any Python files on Ubuntu(WSL)

Time:02-28

I have just started working on my new pc and just to get a feel for it I wanted first to start working on python files, so I started first by just wanting to run WSL on windows and it installed correctly but when I want to run any python using the run python file on the top right on VS code, this is what gets executed $ C:/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe f:/Projects/hello.py And this is the error: -bash: C:/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe: No such file or directory I have no idea what's causing it but when I run the file using 'Shift Enter' which is: Python: Run Selection/Line in Python Terminal it seems to run the single line correctly but it gives me this error instead:

print("Hello, world") -bash: syntax error near unexpected token `"Hello, world"'

but when I run it using python3 hello.py, it works perfectly fine?! I'm so lost as to why this is happening and how could I fix it.

Might be relevant: I'm using windows 10, installed python 3.10.2 from windows store, all of that is in VS code and the python code is one line: print("Hello, world") and I changed the permissions of Local/Microsoft/WindowsApps so it's now accessible by all users to view/read/edit/run, made sure that python3.10.exe exists(on the WindowsApps and it works perfectly) and reinstalled it many times, tired python3.9, and tried to install python from the website instead of the windows store and still the same, manually added python to PATH and tried .venv and didn't work. when I launch python3.10.exe outside vs code it seems to run perfectly, I have worked with python before and it used to work fine now I don't know what's wrong.

I have seen other questions of the same problem I'm having here but none of them solve the problem.

CodePudding user response:

No such file or directory C:/Users/...

For wsl, the Windows filesystem is accessible, but it has a different path. It is mounted under the /mnt folder. So you would find your python .exe under /mnt/c/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe. This said, the executable file is meant to work on Windows, and it doesn't really makes sense to use it on Linux when you could run python within your wsl distro.

python3 works perfectly fine

This is because most Linux distributions come with python3 pre-installed, so you can use it already. To see where it is located, you can run the command which python3, or python3 --version to check its version.

If you want to change version, you may consider download it from you package manager, apt.

I also suggest to install python3-pip if you don't have it already to get the pip package manager for python.

CodePudding user response:

In my case when I ran into this.. I discovered pyenv. This allows you to download more than one version of python. You can then go into a specific directory, such as your python project and issue a python local 3.10.0 (for example). Here's a link on how to install it as well as poetry which is a virtual environment manager that is become very popular. You can also create an alias for python that works off of this. I add this command to my alias file and source it from my .bashrc. alias python='pyenv exec python3'

  • Related