question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attempted Relative import with no known parent package

See original GitHub issue

I would like to be able to import libraries with relative imports (generally I think these are better to avoid namespace conflicts)

However, if I try to do something like:

from .chalicelib.session import session_scope

I get

Traceback (most recent call last):
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/chalice/cli/__init__.py", line 512, in main
    return cli(obj={})
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/chalice/cli/__init__.py", line 205, in deploy
    deployed_values = d.deploy(config, chalice_stage_name=stage)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/chalice/deploy/deployer.py", line 342, in deploy
    return self._deploy(config, chalice_stage_name)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/chalice/deploy/deployer.py", line 348, in _deploy
    self._validate_config(config)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/chalice/deploy/deployer.py", line 371, in _validate_config
    validate_configuration(config)
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/chalice/deploy/validate.py", line 40, in validate_configuration
    routes = config.chalice_app.routes
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/chalice/config.py", line 140, in chalice_app
    app = v()
  File "/c/Users/rjacobs/git/vcf-parser-lambda/venv/lib/python3.6/site-packages/chalice/cli/factory.py", line 258, in load_chalice_app
    app = importlib.import_module('app')
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/c/Users/rjacobs/git/vcf-parser-lambda/src/parser/app.py", line 9, in <module>
    from .chalicelib.models import session_scope
ImportError: attempted relative import with no known parent package

Is it possible to make chalice libs able to be imported with relative imports?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Rubyjcommented, Nov 14, 2019

@stealthycoin Another issue I have noticed is that if you unit test with a package such as pytest the absolute imports from chalicelib will throw errors. Without the whole chalice packaging the imports need to be relative for testing. What is the best way to account for this?

0reactions
stealthycoincommented, Nov 12, 2019

Closing this as I think supporting this would complicate things greatly for no clear gain, it also isn’t the recommended way to import things in general anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

attempted relative import with no known parent package - IQ-Inc
How to fix ImportError: attempted relative import with no known parent package. Learn why it happens and Python import best-practices.
Read more >
Relative imports in Python 3 - Stack Overflow
I was getting this ImportError: attempted relative import with no known parent package. In my program I was using the ...
Read more >
attempted relative import with no known parent package in ...
The solution to this problem is easy. Before advancing to the body of the Python program, programmers first create a Python file with...
Read more >
Importerror attempted relative import with no known parent ...
Importerror attempted relative import with no known parent package error occurs when we import any module from any package (Directory) where __init__.py ...
Read more >
attempted relative import with no known parent package
You are trying to use relative imports in python, but you encounter the exception ImportError: attempted relative import with no known parent package....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found