Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module '' has no attribute '__path__'

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.

like image 391
Blitzkoder Avatar asked Sep 13 '18 16:09

Blitzkoder


People also ask

How do I fix No attribute error in Python?

Attribute errors in Python are raised when an invalid attribute is referenced. To solve these errors, first check that the attribute you are calling exists. Then, make sure the attribute is related to the object or data type with which you are working.

How do you fix an object that has no attribute?

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.

What does module has no attribute mean?

It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.

What is __ init __ PY?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.


1 Answers

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
like image 124
Miguel Silva Avatar answered Sep 22 '22 19:09

Miguel Silva