Closed ghferrari closed 1 month ago
@ghferrari, do you have an __init__.py
your migrations folder?
Hi @cofin,
I think the error above is reproducible if you have a minimal folder/file structure like this:
/parent-folder
/src
app.py
Use my MCVE above as the contents of app.py
. In addition, I use a virtualenv with these packages installed:
advanced_alchemy
alembic
asyncpg
litestar[standard]
redis
sqlalchemy
I also have a postgres database running in docker and set my database URL as an environment variable via export DATABASE_URL="postgresql+asyncpg://name:password@localhost/dbname
In you follow this setup, the db
folder doesn't exist when I run the litestar database init db
command.
For me, this produces this error:
[...]
File "/home/ghf/projects/litestar-test/src/.venv/lib/python3.11/site-packages/alembic/command.py", line 99, in init
script._generate_template(
File "/home/ghf/projects/litestar-test/src/.venv/lib/python3.11/site-packages/alembic/script/base.py", line 593, in _generate_template
util.template_to_file(src, dest, self.output_encoding, **kw)
File "/home/ghf/projects/litestar-test/src/.venv/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 41, in template_to_file
with open(dest, "wb") as f:
^^^^^^^^^^^^^^^^
NotADirectoryError: [Errno 20] Not a directory: '/home/ghf/projects/litestar-test/src/app.py/db/migrations/alembic.ini'
As you can see in the final error line, app.py
is being used (incorrectly) as the name of a directory. This is coming from module_to_os_path
.
@ghferrari I had a chance to re-read this. Let me double check a few things and I'll report back.
@cofin In case it helps, I tried two things:
Use my latest instructions above and then create the .../src/db
folder and add an __init__.py
file in there. Now, when I run litestar database init db
from within the src
folder, I see:
alembic.util.exc.CommandError: Directory db already exists and is not empty
Use my latest instructions above and add an __init__.py
file in .../src
. In that case, I see the original error.
closed by #3565
Description
The file litestar/utils/module_loader.py contains a function definition for
module_to_os_path
.Assuming I've understood the code comments, the purpose of this code is to return the path to a directory, which is either the base directory of the project or (when supplied with the name of a module) the base directory of the module.
Unfortunately, if you define your Litestar object in a file named
app.py
, and there is no other module namedapp
, then this function returns the path to that file. This is already an error given that the intention is to return a path to a directory.I noticed this problem while attempting to set up my own starting configuration following the
litestar-fullstack
repository. This repository defines aBASE_DIR
property at https://github.com/litestar-org/litestar-fullstack/blob/8e6edb90a401778741062a8383ff6e6f354b44dd/src/app/config/base.py#L23 usingmodule_to_os_path
which is then used to define subdirectories for alembic. In my case (using a file namedapp.py
, no module namedapp
), this produces invalid directory pathnames which cannot then be created. For example, when attempting to run as per the MCVE below, I see the error:URL to code causing the issue
https://github.com/litestar-org/litestar/blob/84f51c8afc3203cd4914922b2ec3c1e92d5d40ba/litestar/utils/module_loader.py#L21
MCVE
Steps to reproduce
Screenshots
Logs
No response
Litestar Version
2.9.0
Platform