python相对路径库,Python导入相对路径

I've got a project where I would like to use some python classes located in other directories.

Example structure:

/dir

+../subdirA

+../subdirB

+../mydir

The absolute path varies, because this project is run on different machines.

When my python file with MySampleClass located in /mydir is executed, how do I import OtherClassRoot located in /dir or OtherClassA located in /subdirA?

I tried things like:

from . import MySampleClass as msc

or

from ../ import MySampleClass as msc

but this always fails or gives me error messages like Attempted relative import in non-package

So, whats the right way to relatively import python files?

Any input is highly appreciated :)

解决方案

You will need an __init__.py in the mydir directory (and it can be empty), then as long as dir is in the sys path, assuming your MySampleClass is in myfile.py and myfile.py is in mydir

from mydir.myfile import MySampleClass

If you want to import top level functions from a file called util.py that reside in subdirA into myfile.py (where your class is), then an __init__.py must be in subdirA and then in myfile.py

from subdirA.util import somefunc, someotherfunc

The same is true of the sys path, that is, you must either start from 'dir' or add it. Everything is imported from the top level of the package (usually your project folder).

However, for module testing, where you might run a function from util in the interpreter, if you start from subdirA, you will need to add dir to the sys path, so your imports can resolve.

>>> import sys

>>> sys.path.append('../dir')

but this is a hack and would be preferable to only use from the interpreter when you are testing. You can also add 'dir' to your site packages in a pth file.

To use relative imports, you would need a deeper nested folder, like subdirA/subdirofA, then in subdirofA, you could use . to back out (like from .subdirB ). Really, for me, relative imports are somewhat difficult to see the utility. It's better for me to use direct imports relative to the project directory, but I could see them being useful if you wanted to nest a naive sub package, but again, still better to be explicit than implicit if possible.

also see this

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值