python-3.x 在Windows 10上运行WSL-2 Ubuntu脚本

b4wnujal  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(158)
  • 我正在运行Windows 10企业版(22H2)
  • Ubuntu 20.04通过WSL 2安装
  • 我写了一个Python脚本,在Ubuntu中运行,它接受输入。
  • 我已将Windows任务管理器配置为每天使用以下命令调用脚本一次. C:\Windows\System32\wsl.exe "python3 /mnt/c/Users/.../automatic_temp_to_spreadsheet.py"'
  • 任务编辑器确实启动了任务,一个终端窗口出现在我的屏幕上(根据需要),但是输入的提示并没有出现在窗口中,点击回车键只是关闭了窗口。窗口中没有显示任何内容(甚至没有错误消息)。
    尝试使用上述方法从Windows命令提示符运行脚本失败。它找不到文件,无论我尝试Ubuntu路径还是Windows路径。
C:\>C:\Windows\System32\wsl.exe
user@ubuntumachinename:/$ # This is OK. 

C:\>C:\Windows\System32\wsl.exe "python3"
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> #This is OK too

C:\>C:\Windows\System32\wsl.exe "python3 /mnt/c/Users/.../automatic_temp_to_spreadsheet.py"
/bin/bash: python3 /mnt/c/Users/.../automatic_temp_to_spreadsheet.py: No such file or directory

C:\>C:\Windows\System32\wsl.exe "C:\Users\...\automatic_temp_to_spreadsheet.py"
/bin/bash: C:\Users\...\automatic_temp_to_spreadsheet.py: command not found

请注意,如果我直接从Ubuntu WSL终端窗口运行该脚本,则该脚本可以工作。

user@ubuntumachinename:~$ python3 ./automatic_temp_to_spreadsheet.py
Opening /mnt/c/Users/.../Heat tracking.csv...OK (5)
How many pieces of wood burned today(int)?
(Etc...)
avwztpqn

avwztpqn1#

好的.
答案是在脚本的开头添加一个shebang,并删除开头的“python3”。
所以在python脚本的第一行。。
#!/usr/bin/env python3
然后将命令更改为...
C:\Windows\System32\wsl.exe -e "/mnt/c/Users/.../automatic_temp_to_spreadsheet.py"

相关问题