从父文件夹导入模块 [英] Importing modules from parent folder

查看:88
本文介绍了从父文件夹导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Python 2.5.

I am running Python 2.5.

这是我的文件夹树:

ptdraft/
  nib.py
  simulations/
    life/
      life.py

(我在每个文件夹中也都有__init__.py,为便于阅读,在此省略)

(I also have __init__.py in each folder, omitted here for readability)

如何从life模块内部导入nib模块?我希望无需修补sys.path就可以做到.

How do I import the nib module from inside the life module? I am hoping it is possible to do without tinkering with sys.path.

注意:正在运行的主模块在ptdraft文件夹中.

Note: The main module being run is in the ptdraft folder.

推荐答案

问题似乎与该模块位于父目录或类似内容无关.

It seems that the problem is not related to the module being in a parent directory or anything like that.

您需要将包含ptdraft的目录添加到PYTHONPATH

You need to add the directory that contains ptdraft to PYTHONPATH

您说import nib与您一起工作,这可能意味着您已将ptdraft本身(而不是其父项)添加到PYTHONPATH中.

You said that import nib worked with you, that probably means that you added ptdraft itself (not its parent) to PYTHONPATH.

这篇关于从父文件夹导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆