python导入其他文件夹下的包_在Python 3中从同一包内和包外导入模块

Okay, the scenario is very simple. I have this file structure:

.

├── interface.py

├── pkg

│   ├── __init__.py

│   ├── mod1.py

│   ├── mod2.py

Now, these are my conditions:

mod2 needs to import mod1.

both interface.py and mod2 needs to be run independently as a main script. If you want, think interface as the actual program and mod2 as an internal tester of the package.

So, in Python 2 I would simply do import mod1 inside mod2.py and both python2 mod2.py and python2 interface.py would work as expected.

However, and this is the part I less understand, using Python 3.5.2, if I do import mod1; then I can do python3 mod2.py, but python3 interface.py throws: ImportError: No module named 'mod1' :(

So, apparently, python 3 proposes to use import pkg.mod1 to avoid collisions against built-in modules. Ok, If I use that I can do python3 interface.py; but then I can't python3 mod2.py because: ImportError: No module named 'pkg'

Similarly, If I use relative import:

from . import mod1 then python3 interface.py works; but mod2.py says SystemError: Parent module '' not loaded, cannot perform relative import :( :(

The only "solution", I've found is to go up one folder and do python -m pkg.mod2 and then it works. But do we have to be adding the package prefix pkg to every import to other modules within that package? Even more, to run any scripts inside the package, do I have to remember to go one folder up and use the -m switch? That's the only way to go??

I'm confused. This scenario was pretty straightforward with python 2, but looks awkward in python 3.

UPDATE: I have upload those files with the (referred as "solution" above) working source code here: https://gitlab.com/Akronix/test_python3_packages. Note that I still don't like it, and looks much uglier than the python2 solution.

Related SO questions I've already read:

Related links:

解决方案

TLDR:

Run your code with python -m pkg.mod2.

Import your code with from . import mod1.

The only "solution", I've found is to go up one folder and do python -m pkg.mod2 and then it works.

Using the -m switch is indeed the "only" solution - it was already the only solution before. The old behaviour simply only ever worked out of sheer luck; it could be broken without even modifying your code.

Going "one folder up" merely adds your package to the search path. Installing your package or modifying the search path works as well. See below for details.

But do we have to be adding the package prefix pkg to every import to other modules within that package?

You must have a reference to your package - otherwise it is ambiguous which module you want. The package reference can be either absolute or relative.

A relative import is usually what you want. It saves writing pkg explicitly, making it easier to refactor and move modules.

# inside mod1.py

# import mod2 - this is wrong! It can pull in an arbitrary mod2 module

# these are correct, they uniquely identify the module

import pkg.mod2

from pkg import mod2

from . import mod2

from .mod2 import foo # if pkg.mod2.foo exists

Note that you can always use as to bind your import to a different name. For example, import pkg.mod2 as mod2 lets you work with just the module name.

Even more, to run any scripts inside the package, do I have to remember to go one folder up and use the -m switch? That's the only way to go??

If your package is properly installed, you can use the -m switch from anywhere. For example, you can always use python3 -m json.tool.

echo '{"json":"obj"}' | python -m json.tool

If your package is not installed (yet), you can set PYTHONPATH to its base directory. This includes your package in the search path, and allows the -m switch to find it properly.

If you are in the executable's directory, you can execute export PYTHONPATH="$(pwd)/.." to quickly mount the package for import.

I'm confused. This scenario was pretty straightforward with python 2, but looks awkward in python 3.

This scenario was basically broken in python 2. While it was straightforward in many cases, it was difficult or outright impossible to fix in any other cases.

The new behaviour is more awkward in the straightforward case, but robust and reliable in any case.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值