0

I have a below structure enter image description here

in migrations/env.py file I am trying to import from database import * but it shows no module name database
I tried from ..database imprt * and adding file in pythonpath also but no luck :(

3
  • In which directory are you invoking the interpreter? Commented Dec 10, 2018 at 16:30
  • Wich python version are you using? Commented Dec 10, 2018 at 16:39
  • @RodrigoLópez I am using python 3.6 Commented Dec 11, 2018 at 5:10

2 Answers 2

2

Your directory structure looks a bit suspicious to me. The alembic.ini shouldn't normally be part of the package (and setuptools won't by default pick it up when packaging). I think this would better be placed into the project-root.

Something like this would be more standard:

├── alembic.ini ├── migrations │   ├── env.py │   ├── script.py.mako │   └── versions │ └── ... ├── package_name │   └── database │   ├── __init__.py │ └── ... │   └── models │   └── __init__.py │ └── ... ├── README.md └── setup.py └── ...

Now, this alone would not make database available from env.py. For this to work you have to somehow make your package discoverable. Usually this would be done by installing package_name into some virtualenv. In that environment you could then use from package_name.database import * in your env.py.

Sign up to request clarification or add additional context in comments.

5 Comments

I wanted to say that, but the way I see this is that database has __init__.py
he has __init in database
@deets it doesn't, the submodule models does, but database does not
no, models is > (closed arrow, so the folder contents are not visible for us)
@deets Ah, you're right. the rest still applies though :). I've updated the answer accordingly, thanks
0

Migrations needs to know where to import from, they either belong to the same package:

A:

migrations

database

init.py

And then in migrations:

from A.database.whatever import whatever else

Or you install them as packages separatedly inside your virtualenv: And then each of them is dependent on the other, but because they are installed they can be invoked:

database/setup.py migrations/setup.py

Then both are installed and migrations/env.py can call the installed package database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.