Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imported third party module does not have __file__ attribute

Background

I have done some research on how the __file__ attribute works from the docs (scroll down to 'modules'), and various websites.

From what I understand, all python modules should have a __file__ attribute as long as long as it was loaded from a file.

The Issue

I am installing the third party python module opencafe using

(env)$ pip install opencafe

I am utilizing this test framework.

The issue occurs when I run the test command from the CLI. When that command is run, it triggers this file: env/lib/python2.7/site-packages/cafe/configurator/managers.py.

Within that file there is

import cafe

and later on

_PLUGIN_DIR = os.path.join(os.path.dirname(cafe.__file__), 'plugins')

When this file is run, this error occurs:

AttributeError: 'module' object has no attribute '__file__'

The same error occurs in this scenario:

(env) $ pip install opencafe
Collecting opencafe
  Using cached opencafe-0.3.1-py2.py3-none-any.whl
Installing collected packages: opencafe
Successfully installed opencafe-0.3.1
(env) $ python
Python 2.7.10 (default, Jul 30 2016, 18:31:42)
>>> import cafe
>>> cafe.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'

My Thoughts

My natural inclination is to just believe that this is an issue with opencafe itself, since I am only installing the framework and running a test command from CLI.

However, I wonder if there is an issue with the installation into a virtual environment, or perhaps the PYTHONPATH.

The docs say clearly that "The __file__ attribute is not present for C modules that are statically linked into the interpreter". This module is a python module installed locally. It is not linked to the interpreter like the sys module.

[EDIT]

Here is some more good information about namespace packages.

http://chimera.labs.oreilly.com/books/1230000000393/ch10.html#_discussion_172

The main way that you can tell if a package is serving as a namespace package is to check its __file__ attribute. If it’s missing altogether, the package is a namespace.

Final Update

It turns out that opencafe does not work with the newest versions of virtualenv. It only works at version 15.0.1 or earlier. Reverting back got rid of this error message for me.

When it installed with this version, a __init__.pyc file was created in the package within the virtualenv. This byte compilation is how the __file__ gets its information.

like image 326
gliemezis Avatar asked Oct 21 '25 04:10

gliemezis


2 Answers

That's because opencafe is not a normal python-module. You can easily see this when you open their main module __init__.

The declare_namespace explicitly creates a namespace package. For more informations about this you can visit PEP-382.

like image 101
MSeifert Avatar answered Oct 22 '25 18:10

MSeifert


It's a namespace package, a weird kind of package that can be split across multiple directories. It's not supposed to have a __file__, due to this splitting mechanism.

like image 29
user2357112 supports Monica Avatar answered Oct 22 '25 17:10

user2357112 supports Monica