最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In Python, what is the passing logic of `sys.path` while importing a file or package? - Stack Overflow

programmeradmin0浏览0评论

In a __init__.py file of a package, I imported several files relying on a third party module in another path. To inform python of the path of the third party module, I add the path to sys.path, like this

# __init__.py
import sys
sys.path.append("path of the third party module")

from .file_in_the_package import ... # the file rely on the third party module

Every thing goes fine until I moved the sys.path.append to a separated file in the package, like this

# separated file imp.py
import sys
sys.path.append("path of the third party module")
# changed __init__.py
import imp
from .file_in_the_package import ... # the file rely on the third party module

in which the added "path of the third party module" can not be found in either __init__.py or file_in_the_package.py. However, things goes fine with from .imp import *.

Now, I'm just curious that in what situation the changed sys.path will be passed when import a file/package with sys.path changed?

发布评论

评论列表(0)

  1. 暂无评论