Attempted relative import beyond toplevel package
TLDR: Do
import bash.bosh
or
from bash import bosh
Avoid modifying sys.path
, as this duplicates modules.
When you do
import bosh
it will import the module bosh
. This means Mopy/bash
is in your sys.path
, python finds the file bosh
there, and imports it. The module is now globally known by the name bosh
. Whether bosh
is itself a module or package doesn't matter for this, it only changes whether bosh.py
or bosh/__init__.py
is used.
Now, when bosh
tries to do
from .. import bass
this is not a file system operation ("one directory up, file bass") but a module name operation. It means "one package level up, module bass". bosh
wasn't imported from its package, but on its own, though. So going up one package is not possible - you end up at the package ''
, which is not valid.
Let's look at what happens when you do
import bash.bosh
instead. First, the package bash
is imported. Then, bosh
is imported as a module of that package - it is globally know as bash.bosh
, even if you used from bash import bosh
.
When bosh
does
from .. import bass
that one works now: going one level up from bash.bosh
gets you to bash
. From there, bass
is imported as bash.bass
.
Django. ImportError: attempted relative import beyond top-level package
- import is wrong
from ..iRay_working_with_notes.views import list_notes
should be
from iRay_working_with_notes.views import list_notes
- redirect needs view name from the urls-pattern:
redirect('name-of-my-view-pattern')
So please create a url-pattern entry for the list_notes view and give the pattern name as parameter to the redirect.
Why? because a redirect is telling the browser on the client side to load the target redirect page, so it needs an url (that is generated by django via the url-pattern), as the target page will be called from the browser.
Technically of course you can import a view and call it from another module directly as it is just a python function - but that is not a http redirect (and would not change the url in the browser to the redirected page) and additionally that is not really the idea of the django request/response architecture.
ValueError: attempted relative import beyond top-level package when trying to import models from another app in django
The Python root path is the electron
directory, so you can not work with .from electron.…
You can import the objects by importing it starting with the name of the app, not the project. This thus means that you import this with:
from elec_meter.models import Model1, Model2
while you can make wildcard imports, it is often considered an antipattern, since it is unclear what you import. This means that it can set references to point to objects exported by the elec_meter.models
module, and thus override the original references.
ImportError: attempted relative import beyond top-level package. I am genuinely stuck
replace: from ..Foo import var by from Foo import var
those .. are trying to go beyond top package
\MainDir>dir /b/s
\MainDir\Foo.py
\MainDir\SubDir\Bar.py
Foo.py
global var
var = 'value'
from SubDir import Bar
Bar.py
from Foo import var
print(var)
Pytest throws 'attempted relative import beyond top-level package'
This is what worked:
Inside the thetest.py
file:
from start import app
Related Topics
Change Parent Shell's Environment from a Subprocess
How to Get the Pythonpath in Shell
And' (Boolean) VS '&' (Bitwise) - Why Difference in Behavior with Lists VS Numpy Arrays
Is There a Built-In Function to Print All the Current Properties and Values of an Object
When to Use and When Not to Use Python 3.5 'Await'
What's the Best Way to Parse a JSON Response from the Requests Library
Python List Sort in Descending Order
Get a Variable from the Url in a Flask Route
How to Make Selenium Not Wait Till Full Page Load, Which Has a Slow Script
Fastest Way to Download 3 Million Objects from a S3 Bucket
Request Uac Elevation from Within a Python Script
Remove Duplicate Dict in List in Python
How to Set Time Limit on Raw_Input