无法在 Ubuntu (WSL) 上运行任何 Python 文件
我刚刚开始在我的新电脑上工作,只是为了感受它,我想首先开始处理 python 文件,所以我首先只是想在 Windows 上运行 WSL 并且它安装正确,但是当我想运行任何python 使用 VS code 右上角的 run python 文件,这就是执行的内容 $ C:/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe f:/Projects/hello.python py
这是错误:-bash:C:/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe:没有这样的文件或目录
我不知道是什么原因造成的,但是当我使用 'Shift + Enter'
运行文件时:Python: Run Selection/Line in Python Terminal
它似乎运行单行正确,但它给了我这个错误:
print("Hello, world") -bash:意外标记“Hello, world”附近出现语法错误,
但是当我使用 python3 hello.py
运行它时,它工作得很好?!我很困惑为什么会发生这种情况以及我该如何解决它。
可能相关:我使用的是Windows 10,从Windows商店安装了Python 3.10.2,所有这些都在VS代码中,Python代码是一行: print("Hello, world")
我更改了 Local/Microsoft/WindowsApps
的权限,以便所有用户现在都可以访问它来查看/读取/编辑/运行,并确保 python3.10.exe
存在(在 Windows 应用程序上它工作完美)并重新安装了很多次,累了python3.9
,并尝试从网站而不是Windows商店安装python,仍然相同,手动将python添加到PATH并尝试.venv并没有奏效。当我在 vs code 之外启动 python3.10.exe
时,它似乎运行得很好,我以前使用过 python,它以前工作得很好,现在我不知道出了什么问题。
我已经看到了我在这里遇到的同一问题的其他问题,但没有一个能解决问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有这样的文件或目录
C:/Users/...
对于 wsl,Windows 文件系统是可以访问的,但它有不同的路径。它安装在
/mnt
文件夹下。因此,您可以在/mnt/c/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe
下找到 python .exe。也就是说,可执行文件应该在 Windows 上运行,当您可以在 wsl 发行版中运行 python 时,在 Linux 上使用它并没有真正的意义。python3 工作得很好
这是因为大多数 Linux 发行版都预装了 python3,所以你已经可以使用它了。要查看它所在的位置,您可以运行命令
which python3
或python3 --version
来检查其版本。如果您想更改版本,您可以考虑从包管理器 apt 下载它。
如果您还没有安装 python3-pip 来获取 python 的 pip 包管理器,我还建议您安装它。
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
, orpython3 --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.就我而言,当我遇到这个时..我发现了 pyenv。这允许您下载多个版本的 python。然后,您可以进入特定目录,例如您的 python 项目并发出 python local 3.10.0(例如)。这是关于如何安装它的链接还有诗歌,它是一种非常流行的虚拟环境管理器。您还可以为 python 创建一个以此为基础的别名。我将此命令添加到我的别名文件中,并从我的 .bashrc 中获取它。
alias python='pyenv exec python3'
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'