Python:相对导入意味着你不能自己执行子包吗? [英] Python: Do relative imports mean you can't execute a subpackage by itself?

查看:64
本文介绍了Python:相对导入意味着你不能自己执行子包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近移植了我的Python项目以在Python 3.1上运行。为此我必须在我的项目的子模块和子包中采用相对导入的策略。我不这样做,现在项目本身可以工作,但我注意到我无法执行其中的任何子包或子模块。如果我尝试,我会得到builtins.ValueError:在非包中尝试相对导入。我只能导入整个项目。

I've recently ported my Python project to run on Python 3.1. For that I had to adopt the policy of relative imports within the submodules and subpackages of my project. I've don’t that and now the project itself works, but I noticed I can't execute any of the subpackages or submodules in it. If I try, I get "builtins.ValueError: Attempted relative import in non-package". I can only import the whole project.

这是正常的吗?

推荐答案

你可以使用python解释器的-m标志来运行子包中的模块(甚至3.1中的包。)。

You can use -m flag of the python interpreter to run modules in sub-packages (or even packages in 3.1.).

这篇关于Python:相对导入意味着你不能自己执行子包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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