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

python - Import error for direct execution of __init__.py in package - Stack Overflow

programmeradmin2浏览0评论
import sys

sys.path.append(".")
from . import module

Package structure:

TestPackage

|-> init.py

|-> module.py

from . import module

ImportError: attempted relative import with no known parent package

Direct command line execution: python PATH_TO_INIT.PY gives the above error Anyone know why im getting this error?

import sys

sys.path.append(".")
from . import module

Package structure:

TestPackage

|-> init.py

|-> module.py

from . import module

ImportError: attempted relative import with no known parent package

Direct command line execution: python PATH_TO_INIT.PY gives the above error Anyone know why im getting this error?

Share Improve this question asked 2 days ago Pacific EastPacific East 111 bronze badge New contributor Pacific East is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

Directly executing a package's __init__.py isn't expected to work. Whatever goal you were hoping to achieve by doing that, you should find a different way to go about it.

If you try to do it anyway, it won't be considered the package's __init__.py when you do that. It'll just be considered some random .py file, not part of a package at all, and you can't perform relative imports from some random .py file that isn't part of a package.

Also, your import path will be messed up, and sys.path.append(".") doesn't fix it.

发布评论

评论列表(0)

  1. 暂无评论