从 wsl shell 运行 .py 文件

问题描述 投票:0回答:1

我正在尝试从 shell 执行一个简单的

hello.py
文件(实际上是 wsl),但显然它不起作用。我使用以下命令来确保位于包含
.py
文件的目录中:

import os
os.getcwd()

错误总是如下:

NameError:名称'hello.py'未定义。

有什么建议吗?

我尝试了很多可能的解决方案,但没有一个有效。 例如我尝试编写 python3 hello.py 但没有成功。 在此输入图片描述

python python-3.x shell windows-subsystem-for-linux python-import
1个回答
0
投票

所以基本上Python已经安装在WSL中了。如果你在 Windows 文件夹中有一个 hello.py,即

C:/Users/<user>/Desktop/hello.py

那么你需要知道 wsl 有不同的文件夹结构。如果您想从 Windows 访问该文件,则其路径从

开始
/mnt/c/Users/<user>/Desktop/hello.py

enter image description here

假设你走在正确的道路上,尝试跑步

python3 ./hello.py

python ./hello.py

我希望你觉得这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.