Python - ATBS Appendix B - Can't find '__main__' module in C:\\ path error

Viewed 601

I am trying to complete Appendix B in Automate the Boring Stuff - 'Running Python Programs on Windows' however when I WIN-R the script and argv, I get the error "can't find '__ main __' module in C:\ path.

I have created the .py script as well as the batch file, changed the system variable path but still can't get the program to run from WIN-R.

My script for pw.py is as follows:

#! /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) 

My pw.bat file is as follows:

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

When I run pw email in WIN-R, I get the following error: C:\Users\lukev\AppData\Local\Programs\Python\Python38-32\python.exe: can't find '__main__' module in 'C:\\Users\\lukev\\PythonScripts'

From my research I found out the shebang line shouldn't be as described in the book and should instead be #! /usr/bin/env python3 and another possibility be if I had multiple versions of Python installed however I don't have other versions installed and still have the issue.

Below are screenshots of the python file, the batch file, system environment variables, and error message:

pw.py

pw.bat

System variables

error message

1 Answers

You use

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

in the batch-file. The path being passed to py.exe is a folder path.

This creates an error:

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

The error is precise. The path is a folder path, so what Python does is look for an entry point. As displayed, that entry point is __main__.py. If it cannot find the entry point, it displays the error message.

If you want to execute a file, then execute it direct:

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

To understand the __main__ module entry point, create a folder named C:\PythonExecutable and create a file inside the directory named __main__.py. Insert into this file the following code:

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__)

Enter some commands in a Command Prompt:

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:\>

A shebang line in __main__.py is not applied as py.exe does not read from this file.

Related