Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import doesn't notice changes to working directory #53201

Closed
purpleidea mannequin opened this issue Jun 9, 2010 · 2 comments
Closed

import doesn't notice changes to working directory #53201

purpleidea mannequin opened this issue Jun 9, 2010 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@purpleidea
Copy link
Mannequin

purpleidea mannequin commented Jun 9, 2010

BPO 8955
Nosy @bitdancer
Files
  • import_bug.tar: Tarball of necessary files for testing
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-06-09.17:39:55.168>
    created_at = <Date 2010-06-09.15:44:14.310>
    labels = ['invalid', 'type-bug', 'library']
    title = "import doesn't notice changes to working directory"
    updated_at = <Date 2010-06-09.17:39:55.163>
    user = 'https://bugs.python.org/purpleidea'

    bugs.python.org fields:

    activity = <Date 2010-06-09.17:39:55.163>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-06-09.17:39:55.168>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2010-06-09.15:44:14.310>
    creator = 'purpleidea'
    dependencies = []
    files = ['17600']
    hgrepos = []
    issue_num = 8955
    keywords = []
    message_count = 2.0
    messages = ['107403', '107407']
    nosy_count = 2.0
    nosy_names = ['r.david.murray', 'purpleidea']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue8955'
    versions = ['Python 2.6', 'Python 3.1']

    @purpleidea
    Copy link
    Mannequin Author

    purpleidea mannequin commented Jun 9, 2010

    Attempting to change the working directory and then import based on that change has no effect. Import seems impossible. Attached is tarball example. As seen below, bar1.py can import foo from src, however bar2.py bar3.py and bar4.py cannot, despite their respective scripts changing their cwd. Below is results of a terminal session. Thanks in advance.

    james@hostname:~$ tree import_bug/
    import_bug/
    |-- bar1.py
    |-- init.py
    |-- src
    | |-- foo.py
    | -- __init__.py -- test
    |-- bar2.py
    |-- bar3.py
    |-- bar4.py
    |-- bar5.py
    `-- init.py

    2 directories, 9 files
    james@hostname:~$ cat import_bug/src/foo.py
    # this is foo.py
    print('this is foo.py')

    james@hostname:~$ cat import_bug/bar1.py
    # this is bar1.py
    import os

    print(os.getcwd())
    from src import foo
    print('this is bar1.py')

    james@hostname:~$ cat import_bug/test/bar2.py
    # this is bar2.py
    import os

    os.chdir(os.path.join(os.path.dirname(file), os.pardir))

    print(os.getcwd())
    from src import foo
    print('this is bar2.py')
    james@hostname:~$ python import_bug/bar1.py 
    /home/james
    this is foo.py
    this is bar1.py
    james@hostname:~$ python import_bug/test/bar2.py 
    /home/james/import_bug
    Traceback (most recent call last):
      File "import_bug/test/bar2.py", line 7, in <module>
        from src import foo
    ImportError: No module named src
    james@hostname:~$

    @purpleidea purpleidea mannequin added the stdlib Python modules in the Lib dir label Jun 9, 2010
    @bitdancer
    Copy link
    Member

    This is working as designed.

    Try printing sys.path in your scripts. It is what's in sys.path that matters, not the cwd. (The cwd is put in the path as "" in the specific case of running the python interactive shell...and in certain applications that embed Python because of a bug in those applications.)

    @bitdancer bitdancer added invalid type-bug An unexpected behavior, bug, or error labels Jun 9, 2010
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant