Skip to content

prepend_sys_path does not work with absolute paths on windows #1330

Closed

Description

@mwerezak

Describe the bug

If you put an absolute path in prepend_sys_path in your alembic.ini on windows (which will happen if you use %(here)s), the path will get split on the ':' character that separates the drive letter.

Expected behavior

Absolute paths to not get broken.

To Reproduce
Use an absolute path in prepend_sys_path like C:/something or %(here)s/something

Then print out sys.path, e.g. for path in sys.path: print(path) you get:

C
\something
C
\path\to\here\something
# Insert code here

Error
ModuleNotFoundError: No module named ...
because env.py can't import my modules.

Versions.

  • OS: Windows 10
  • Python: 3.11.5
  • Alembic: 1.12.0
  • SQLAlchemy: 1.4.49
  • Database: Postgres
  • DBAPI: psycopg2

Additional context

prepend_sys_path = config.get_main_option("prepend_sys_path")
if prepend_sys_path:
sys.path[:0] = list(
_split_on_space_comma_colon.split(prepend_sys_path)
)

Activity

  1. zzzeek commented on Oct 19, 2023

    @zzzeek
    Member

    @CaselIT is this also #1284 ?

  2. added
    bugSomething isn't working
    and removed
    requires triageNew issue that requires categorization
    on Oct 19, 2023
  3. zzzeek commented on Oct 19, 2023

    @zzzeek
    Member

    PRs with tests welcome here

  4. added a commit that references this issue on Oct 19, 2023
    a4b3782
  5. CaselIT commented on Oct 19, 2023

    @CaselIT
    Member

    @CaselIT is this also #1284 ?

    it should not, since that depend on the value of version_path_separator

  6. zzzeek commented on Oct 19, 2023

    @zzzeek
    Member

    Proposed in #1331 we add a new config value with legacy fallback behavior as follows:

    # sys.path path, will be prepended to sys.path if present.
    # defaults to the current working directory.  for multiple paths, the path separator
    # is defined by prepend_sys_path_separator
    prepend_sys_path = .
    
    # prepend sys path separator; As mentioned above, this is the character used to split
    # sys_path paths.  The default within new alembic.ini files is "os", which uses os.pathsep.
    # If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
    # Valid values for prepend_sys_path_separator are:
    #
    # prepend_sys_path_separator = :
    # prepend_sys_path_separator = ;
    # prepend_sys_path_separator = space
    prepend_sys_path_separator = os  # Use os.pathsep. Default configuration used for new projects.
    
    
  7. CaselIT commented on Oct 19, 2023

    @CaselIT
    Member

    it's probably the most sensible option. it makes little sense to have the two paths in the config handled in a different way

  8. zzzeek commented on Oct 19, 2023

    @zzzeek
    Member

    I guess if we were on pyproject.toml this whole thing would be a non issue since it supports lists :)

  9. CaselIT commented on Oct 19, 2023

    @CaselIT
    Member

    does not support %(here)s though

  10. added a commit that references this issue on Apr 2, 2025
    2383617
  11. sqla-tester commented on Apr 26, 2025

    @sqla-tester
    Collaborator

    Mike Werezak has proposed a fix for this issue in the main branch:

    Improve handling of prepend_sys_path, fixes #1330 https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/5873

  12. added a commit that references this issue on Jun 11, 2025
    9913fdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions