Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python -m causes "Error while finding module specification for"

Tags:

python

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?


1 Answers

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.

like image 123
borancar Avatar answered Jul 24 '26 22:07

borancar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!