creating a utility script inside a django app; relative import woes

10 views
Skip to first unread message

Jake Waxman

unread,
May 18, 2020, 9:39:16 PM5/18/20
to Django users
I'm a relatively new Django user.
I'm running into a problem related to relative imports.
I have a project and app set up the usual way

mysite
-- myapp
---- __init__.py
---- all the modules and templates in myapp including one that contains the definition of MyClass
-- mysite
---- __init__py
---- all of the other the standard stuff inside mysite

The project works.

I want to create a utility script that makes use of a module in myapp
I want to be able to run this utility script from the command line.

When I try to import MyClass from mymodule via
from .mymodule import MyClass

I get
ImportError: attempted relative import with no known parent package

If I leave out the dot before mymodule
I get the same error but for another module in myapp that mymodule tries to import,

I suspect I'm trying to do something I'm not supposed to do but don't know what.
If it helps, I'm doing this in vscode


Reply all
Reply to author
Forward
0 new messages