Python:在中找不到'__main__'模块

时间:2020-10-19 20:47:45

标签: python

我已经有了要从Windows Run运行的python代码。但是,当我尝试运行它时,cmd会显示此消息。

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

我正在使用VS Code在python 3.8.3 32位虚拟环境中编写程序。 python程序称为pw.py,批处理文件称为pw.bat

pw.py

C:\ Users \ myName \ OneDrive-companyName \ 04编程\ 01 Visual Studio代码\ LearningPython \ pw.py

#! python3
# pw.py - An insecure password locker program.

import sys, pyperclip

PASSWORD = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
            'blog': 'VmAlvQyKAxiVH5G8vo1if1MLZF3sdt',
            'luggage': '12345'}

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

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

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

pw.bat

C:\ Users \ myName \ MyPythonScripts

@py.exe C:\Users\myName\OneDrive - companyName\04 Programming\01 Visual Studio Code\LearningPython\pw.py %*
@pause

1 个答案:

答案 0 :(得分:0)

首先,检查是否正确处理了空格,这不是很明显。尝试在批处理文件中用反斜杠( )前缀空格(\ ),

@py.exe C:\Users\myName\OneDrive\ -\ companyName\04\ Programming\01\ Visual\ Studio\ Code\LearningPython\pw.py %*
@pause

另一种方法是将路径用引号引起来,即:

@py.exe "C:\Users\myName\OneDrive - companyName\04 Programming\01 Visual Studio Code\LearningPython\pw.py" %*
@pause

您还可以考虑添加以下内容:

if __name__ == "__main__":
    #code here

使用PASSWORDS字典后,即:

#! python3
# pw.py - An insecure password locker program.

import sys, pyperclip

PASSWORD = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
            'blog': 'VmAlvQyKAxiVH5G8vo1if1MLZF3sdt',
            'luggage': '12345'}

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

    # And so on...

您可以详细了解此herethere