Python ATBS附录B在C:\\path error中找不到“\uuuu main\uuuu”模块

2024-03-29 10:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图完成附录B中的自动化枯燥的东西-“在Windows上运行Python程序”,但是当我赢得脚本和argv时,我在C:\path中得到了错误“找不到”\uuuuuuu_; main\u___;模块

我已经创建了.py脚本和批处理文件,更改了系统变量路径,但仍然无法从WIN-R运行程序

我的pw.py脚本如下:

#! /usr/bin/env python3
# pw.py - An insecure password locker program.

PASSWORDS = {'email': 'F7min1BDDuvMJuxESSKHFhTxFtjVB6',
                'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
                'luggage': '12345'}

import sys
import pyperclip

if len(sys.argv) < 2:
    print('Usage: python pw.py [account] - copy account password')
    sys.exit()

account = sys.argv[1]   #first command line arg is the account name

if account in PASSWORDS:
    pyperclip.copy(PASSWORDS[account])
    print('Password for ' + account + ' copied to clipboard.')
else:
    print('There is no account named ' + account) 

我的pw.bat文件如下:

@py.exe C:\Users\lukev\PythonScripts %*
@pause

在WIN-R中运行pw email时,出现以下错误: C:\Users\lukev\AppData\Local\Programs\Python\Python38-32\python.exe: can't find '__main__' module in 'C:\\Users\\lukev\\PythonScripts'

从我的研究中,我发现shebang行不应该像书中描述的那样,而应该是#! /usr/bin/env python3,另一种可能是如果我安装了多个版本的Python,但是我没有安装其他版本,并且仍然存在问题

下面是python文件、批处理文件、系统环境变量和错误消息的屏幕截图:

pw.py

pw.bat

System variables

error message


Tags: 文件py程序脚本main系统错误sys
1条回答
网友
1楼 · 发布于 2024-03-29 10:27:28

你用

@py.exe C:\Users\lukev\PythonScripts %*

在批处理文件中。传递给py.exe的路径是文件夹路径

这会产生一个错误:

C:\Users\lukev\AppData\Local\Programs\Python\Python38-32\python.exe: can't find '__main__' module in 'C:\\Users\\lukev\\PythonScripts'

误差是精确的。路径是一个文件夹路径,因此Python所做的就是查找入口点。如图所示,该入口点为__main__.py。如果找不到入口点,则显示错误消息

如果要执行文件,请直接执行:

@py.exe C:\Users\lukev\PythonScripts\pw.py %*

要理解__main__模块入口点,请创建一个名为C:\PythonExecutable的文件夹,并在名为__main__.py的目录中创建一个文件。在该文件中插入以下代码:

import sys

if __name__ == '__main__':

    # Check command line arguments.
    if len(sys.argv) > 1:
        if sys.argv[1] == '-h':
            print('I am here to help')
        else:
            for index, item in enumerate(sys.argv):
                print(index, item)
    else:
        print('I am ', __name__)

在命令提示符中输入一些命令:

C:\> py PythonExecutable
I am  __main__

C:\> py PythonExecutable -h
I am here to help

C:\> py PythonExecutable arg1 arg2 arg3 "I am the fourth"
0 PythonExecutable
1 arg1
2 arg2
3 arg3
4 I am the fourth

C:\>

由于py.exe未从该文件读取数据,因此__main__.py中的shebang行未应用

相关问题 更多 >