Getting Attempted relative import in non-package error in spite of having __init__.py

AppleGrew

I have a package cclogger. This directory has a __init__.py file with some code to load the configuration. When I try to run the file api_main.py in that directory using the following command...

python -m cclogger.api_main

I get the following erro:-

config loaded
Instantiating DB with: cclogger/test123@localhost:x
Instantiated ParseCentral
Register parser called by : CitiIndia
Registered parser for email:  [email protected]
Instantiated SmsParseCentral
Register parser called by : Citi Bank
Registered sms parser for address:  lm-citibk
Register parser called by : HDFC Bank
Registered sms parser for address:  am-hdfcbk
Traceback (most recent call last):
  File "/Users/applegrew/Dropbox/Credit Expense/cclogger/cclogger/api_main.py", line 4, in <module>
    from .bottle import run, default_app, debug, get
ValueError: Attempted relative import in non-package

The messages displayed above the error are from modules in the same package which were imported by __init__.py.

The code in api_main.py is:-

import re
import os

from .bottle import run, default_app, debug, get
from .common_util import date_str_to_datetime, UTCOffset, date_filter

#app = Bottle()

default_app().router.add_filter('date', date_filter)

from . import api, dev

@get('/index')
def index():
    return "CCLogger API main live and kicking."

if dev:
    debug(True)
    run(reloader=True, port=9000)
else:
    os.chdir(os.path.dirname(__file__))
    application = default_app()

I have python 2.7.1.

What am I doing wrong? You can see the full code at https://github.com/applegrew/cclogger/tree/master/cclogger

lucasg

You cannot run a python module directly as a script (I don't really know the reason why).

EDIT : The reason is explained in the PEP338 which is the spec for the "-m" option.

The release of 2.5b1 showed a surprising (although obvious in retrospect) interaction between this PEP and PEP 328 - explicit relative imports don't work from a main module. This is due to the fact that relative imports rely on __name__ to determine the current module's position in the package hierarchy. In a main module, the value of __name__ is always __main__ , so explicit relative imports will always fail (as they only work for a module inside a package).

For the 2.5 release, the recommendation is to always use absolute imports in any module that is intended to be used as a main module

To test your application, encapsulate api_main in a function and create a top-level main.py file which will run the main loop :

cclogger/api_main.py :

import re
import os


from .bottle import run, default_app, debug, get
from .common_util import date_str_to_datetime, UTCOffset, date_filter

#app = Bottle()


def main():
    default_app().router.add_filter('date', date_filter)

    from . import api, dev

    @get('/index')
    def index():
        return "CCLogger API main live and kicking."

    if dev:
        debug(True)
        run(reloader=True, port=9000)
    else:
        os.chdir(os.path.dirname(__file__))
        application = default_app()

And /main.py :

from cclogger import api_main


if __name__ == '__main__':
    api_main.main()

You can run your application by typing python main.py, python -m main or python -c "import cclogger.api_main; api_main.main()" .

PS : thanks for linking the complete source, it's always much more helpful than the stubs provided with the question.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to fix "Attempted relative import in non-package" even with __init__.py

From Dev

Attempted relative import in non-package error in current structure

From Dev

ValueError: Attempted relative import in non-package not for tests package

From Dev

Python3: Attempted relative import in non-package

From Dev

Python3: Attempted relative import in non-package

From Java

Attempted relative import with no known parent package

From Dev

Getting "ImportError: attempted relative import with no known parent package" when running from Python Interpreter

From Dev

ValueError: Key font.fantasy: Attempted relative import in non-package in Mercurial

From Dev

relative import from __init__.py file throws error

From Dev

Why do I have to use a relative import in a package's __init__.py?

From Dev

Relative import error with py2exe

From Dev

Flask __init__.py import error

From Dev

Python Import class in __init__.py from file in same package

From Dev

How to import a class defined inside __init__.py in same package?

From Dev

How to import a class defined inside __init__.py in same package?

From Java

Receiving "Attempted import error:" in react app

From Java

In VS Code, I'm getting this error, 'Failed to load module. Attempted to load prettier from package.json'

From Dev

Getting error running SSIS package on non-SSIS Server

From Dev

Does import __foo__ import from the __init__ file of the foo package?

From Dev

Circular import error while organising __init__.py files

From Dev

Circular import error while organising __init__.py files

From Dev

Attempted import error: 'getMoviesList' is not exported from './actions'; error in react redux

From Dev

Getting an "invalid syntax" error from __init__.py

From Dev

Getting an "invalid syntax" error from __init__.py

From Dev

Unknown python import behaviour from relative package

From Dev

Python: How to import package root with relative imports

From Dev

Golang relative package import after renaming

From Dev

Python Relative Import cannot find package

From Dev

Attempted import error: 'default' is not exported from '../assertThisInitialized/_index.mjs'

Related Related

  1. 1

    How to fix "Attempted relative import in non-package" even with __init__.py

  2. 2

    Attempted relative import in non-package error in current structure

  3. 3

    ValueError: Attempted relative import in non-package not for tests package

  4. 4

    Python3: Attempted relative import in non-package

  5. 5

    Python3: Attempted relative import in non-package

  6. 6

    Attempted relative import with no known parent package

  7. 7

    Getting "ImportError: attempted relative import with no known parent package" when running from Python Interpreter

  8. 8

    ValueError: Key font.fantasy: Attempted relative import in non-package in Mercurial

  9. 9

    relative import from __init__.py file throws error

  10. 10

    Why do I have to use a relative import in a package's __init__.py?

  11. 11

    Relative import error with py2exe

  12. 12

    Flask __init__.py import error

  13. 13

    Python Import class in __init__.py from file in same package

  14. 14

    How to import a class defined inside __init__.py in same package?

  15. 15

    How to import a class defined inside __init__.py in same package?

  16. 16

    Receiving "Attempted import error:" in react app

  17. 17

    In VS Code, I'm getting this error, 'Failed to load module. Attempted to load prettier from package.json'

  18. 18

    Getting error running SSIS package on non-SSIS Server

  19. 19

    Does import __foo__ import from the __init__ file of the foo package?

  20. 20

    Circular import error while organising __init__.py files

  21. 21

    Circular import error while organising __init__.py files

  22. 22

    Attempted import error: 'getMoviesList' is not exported from './actions'; error in react redux

  23. 23

    Getting an "invalid syntax" error from __init__.py

  24. 24

    Getting an "invalid syntax" error from __init__.py

  25. 25

    Unknown python import behaviour from relative package

  26. 26

    Python: How to import package root with relative imports

  27. 27

    Golang relative package import after renaming

  28. 28

    Python Relative Import cannot find package

  29. 29

    Attempted import error: 'default' is not exported from '../assertThisInitialized/_index.mjs'

HotTag

Archive