15

I'm having a problem which I have no idea how to further debug.

I have a project with different purposes, making use of Python 3 among other things. I created a Python package named package. The package's top-directory is located inside myproject/python/. In the filesystem, it has the following structure:

- /home/myuser/myproject/python
--- package/
------ __init__.py
------ myutil.py
------ sub_package/
---------- __init__.py
---------- sub_sub_package/
-------------- __init__.py
-------------- myscript.py

All __init__.py files are blank, with the exception of the root one (package/__init__.py), which has the following contents:

from . import myutil

So far so good. The file myscript.py is actually a Python script to run directly. As it resides inside a package, I am executing it as such:

cd /home/myuser/myproject/python
python -m package.sub_package.sub_sub_package.myscript

Now the weird part. The script works as expected. However, after the program finishes, I get the following message:

/usr/bin/python3: Error while finding module specification for 
'package.sub_package.sub_sub_package.myscript.py'
(AttributeError: module 'package.sub_package.sub_sub_package.myscript' 
has no attribute '__path__')

I have been searching online but to no avail. Can't figure out what is causing this message and how to solve it. I am guessing it is some obscure behavior of Python 3's import handling, but have no clue. Any help is greatly appreciated.

Blitzkoder
  • 1,768
  • 3
  • 15
  • 30
  • Take a look at the solution in this thread -> https://stackoverflow.com/questions/54030519/python-tornado-attributeerror-module-test-has-no-attribute-path Cheers – Corkscrew Jul 24 '19 at 13:37

2 Answers2

9

Two ways to run a python 3 script with a filename 'fibo.py':

The argument is the name of the .py file.

python fibo.py

The argument is the name of a Python module, without .py

python -m fibo
Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23
1

you do not have __init__.py file in the last directory sub_sub_package

try adding an empty __init__.py file there

Landar
  • 278
  • 1
  • 10
  • 6
    I have done that, the behavior appears to be the same. – Blitzkoder Sep 13 '18 at 16:15
  • 1
    this seems similar to this issue https://stackoverflow.com/questions/36230492/python-error-while-finding-spec-for-fibo-py-class-attributeerror-mod – Landar Sep 13 '18 at 16:21
  • 1
    Well I had seen that one already. The major concern in the answer of that issue was to use the **-m** flag, which I am doing already. – Blitzkoder Sep 13 '18 at 16:25