0

I had a directory structure like the following for a Python project:

my_project
|_ 
   ... 
   dirA
      |_ 
        __init__.py
        scriptA.py
   dirB
      |_
        __init__.py
        scriptB.py
   ...

Inside __init__.py of dirA (__init__.py of dirB is empty), I have:

import os
import glob
__all__ = [os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(os.path.abspath(__file__)) + "/*.py")]

Inside scriptB.py, I do:

from dirA import *

And the subsequent code which utilizes the imports works fine.

Then, I copied and pasted the whole project (with a different project directory name: my_project_new) in the same directory as the old one, and I start changing the code of scriptA.py in this newly duplicated directory. (Note: directory structure of my_project_new remains exactly the same as above).

After making the changes, I tried to run scriptB.py of my_project_new, and I found that it kept importing dirA from the original my_project directory, instead of the dirA in the current my_project_new directory. Why is this?

Further, I moved the original my_project folder to a different location, and tried to run scriptB.py of my_project_new again. It then failed to identify dirA altogether. What is happening now?

4

1 回答 1

2

我会发表评论,但我没有足够的声誉。希望我的猜测是正确的。

__init__.pyc文件吗?也许当它编译字节码时,它会存储导入的绝对路径。尝试删除 .pyc 文件并导入。

于 2012-07-27T20:56:52.987 回答