3

I've got this python code where I would like to run from Windows Run. However, the cmd displays this message when I try an run it.

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

I am using VS Code to write my program with a python 3.8.3 32-bit virtual enviroment. The python program is called pw.py and the batch file is called pw.bat

pw.py

C:\Users\myName\OneDrive - companyName\04 Programming\01 Visual Studio Code\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
newToCoding
  • 174
  • 1
  • 1
  • 8

1 Answers1

7

First, check whether you handle spaces correctly, it's not that obvious. Try to prefix spaces ( ) with backslash (\ ) in your batch file like that:

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

Another way to do that is to enclose the path in quotation marks, i.e.:

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

Also you can consider to add the following:

if __name__ == "__main__":
    #code here

after you use PASSWORDS dict, i.e.:

#! 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...

You can read more about this here or there

mrkhrova
  • 86
  • 5
  • 1
    this is seriously under-upvoted IMO. anyone care to pay this answer some homage? it was literally "0" when I came to this page, which is unacceptable for the powerful use-cases loaded up in this answer. Of course feel free to disagree, but if you don't, pay some homage please! IMO, stellar answer good sir! – acat Jan 31 '22 at 09:35
  • I kicked it up one as it is valid. I was looking at a different issue as I am new to pyCharm. Apparently this appears it I run the project with the green arrow in the upper right corner. OTOH, if I highlight the line 'if __name__ == "__main__":', and right click and run that way, I don't see the error. – Jamie Oct 08 '22 at 13:22