5

The doc says

Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line

I wrote this code to help myself understand the -m

def print_hi(n=1):
    print('hi'*n)

if __name__ == "__main__":
    print_hi(9)

I saved this as a file aname.py.

And then I ran this command, and get the expected result.

$ python aname.py 
hihihihihihihihihi

question

When I execute the file as a module, this error shows up

$ python -m aname.py
/usr/bin/python: Error while finding module specification for 'aname.py' (AttributeError: module 'aname' has no attribute '__path__')

What causes this error? How to fix it?

  • [What does it mean to “run library module as a script” with the “-m” option?](https://stackoverflow.com/questions/46319694/what-does-it-mean-to-run-library-module-as-a-script-with-the-m-option) might be helpful as well. – Trenton McKinney Sep 03 '19 at 16:15

1 Answers1

12

You're supposed to use the m without the .py suffix, i.e.: $ python -m aname

From the man page:

       -m module-name
              Searches sys.path for the named module and runs the corresponding .py file as
              a script.

The m parameter is a module, similar to import or from.

borancar
  • 1,109
  • 8
  • 10