This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: import doesn't notice changes to working directory
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: purpleidea, r.david.murray
Priority: normal Keywords:

Created on 2010-06-09 15:44 by purpleidea, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
import_bug.tar purpleidea, 2010-06-09 15:44 Tarball of necessary files for testing
Messages (2)
msg107403 - (view) Author: James (purpleidea) Date: 2010-06-09 15:44
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:~$
msg107407 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-06-09 17:39
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.)
History
Date User Action Args
2022-04-11 14:57:02adminsetgithub: 53201
2010-06-09 17:39:55r.david.murraysetstatus: open -> closed

type: behavior

nosy: + r.david.murray
messages: + msg107407
resolution: not a bug
stage: resolved
2010-06-09 15:44:14purpleideacreate