不清楚如何设置__package__ __变量
我有一个Django项目,已经工作了一段时间。我想到要在Postgres数据库中加载大量数据的地步。我为使用SQLite3的新的Django项目搁置了一个新的Django项目,只是为了制定代码并运行一些测试,以确保所有内容都正确插入。
直到我真正想测试插入脚本的地步,一切似乎都很好。
ImportError: attempted relative import with no known parent package
该错误是指脚本中的这一行:
from .models import Statute
截至目前,这没有红色的线条。 Model.py和此脚本在同一目录中彼此相处:
$ tree
└── parsecodetree
├── db.sqlite3
├── manage.py
├── parsecodetree
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── ustitles
├── admin.py
├── apps.py
├── __init__.py
├── insert504.py
├── insert_script_results
├── migrations
├── models.py
├── t10p1b.html
├── tests.py
└── views.py
我在旧的David Beazely视频中发现了这个技巧:
parsecodetree/ustitles/__init__.py:
from .models import *
print("This is ustitles.__init__")
“This is ustitles.__init__” does not print.
评论或无意义的“来自.models Import *”似乎没有什么区别。
但是:
/home/malikarumi/Projects/uscode/parsecodetree/parsecodetree/__init__.py:
print("This is parsecodetree.__init__")
"This is parsecodetree.__init__" does print.
但是显然它也会遇到一个错误:
This is parsecodetree.__init__
Traceback (most recent call last):
<...snip…>
ModuleNotFoundError: No module named 'parsecodetree.parsecodetree'
我从Realpython.com发现了这一点,更令人困惑,而不是有用。如果不需要__ INIT __。py
,为什么我什至要开始这个问题?
https://realpython.com/package-initialization/
注意:大部分Python文档指出
__ INIT __。py
文件 创建软件包时,必须在包装目录中存在。这 曾经是真的。过去,__ init __。py
的存在。 向Python表示正在定义包裹。文件可以 包含初始化代码甚至是空的,但必须是 现在。从Python 3.3开始,引入了隐式命名空间软件包。 这些允许在没有任何
__ INT __。py
的情况下创建软件包。 文件。当然,如果包装初始化是 需要。但不再需要。
我看到了很多有关设置__软件包__
变量的内容。但是我要把这项任务放在哪里呢?我尝试了设置。那没用。
我不了解从Python“包”到Django“ Project”的映射 base_dir ==“ project”,但都==“软件包”或“ __软件包__
”?
在我的设置中,parsecodetree和ustitles既有“软件包”吗?我是否将它们设置为== __软件包__
?他们是否有__ INIT __。py
?
因此,此帖子使每个目录似乎都需要__ INIT __。py
,而Realpython课程的相反。它似乎也不干。
这会有红色的线条:
from ustitles.models import Statute
还有一个值得启动的追溯:
This is ustitles.__init__
Traceback (most recent call last):
<...snip…>
ModuleNotFoundError: No module named 'parsecodetree.parsecodetree'
但是您注意到吗?现在“ 这是ustitles .__ Init __
”打印!
同样,如何使用pycharm“运行”运行“ -m”?事实证明,这仍然是一个空旷的问题: https://intellij-support.jetbrains.com/hc/hc/en-us/community/community/posts/posts/360003879119-how-posts- m-command-in-in-pycharm-?page = 1##community_comment_5925736517010
I have a Django project that I've been working on for a while. I came to the point where I wanted to load a significant amount of data into my Postgres database. I set aside a new Django project with sqlite3 just to work out the code and run some tests to make sure everything was inserted properly.
Everything seemed fine until I got to the point where I actually wanted to test the insert script.
ImportError: attempted relative import with no known parent package
The error refers to this line in the script:
from .models import Statute
As of right now, this has no red squiggly line on it. models.py and this script are right beside each other in the same directory:
$ tree
└── parsecodetree
├── db.sqlite3
├── manage.py
├── parsecodetree
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── ustitles
├── admin.py
├── apps.py
├── __init__.py
├── insert504.py
├── insert_script_results
├── migrations
├── models.py
├── t10p1b.html
├── tests.py
└── views.py
I found this trick in an old David Beazely video:
parsecodetree/ustitles/__init__.py:
from .models import *
print("This is ustitles.__init__")
“This is ustitles.__init__” does not print.
Commenting or uncommenting ‘from .models import *’ seems to make no difference.
However:
/home/malikarumi/Projects/uscode/parsecodetree/parsecodetree/__init__.py:
print("This is parsecodetree.__init__")
"This is parsecodetree.__init__" does print.
But apparently it also gets an error:
This is parsecodetree.__init__
Traceback (most recent call last):
<...snip…>
ModuleNotFoundError: No module named 'parsecodetree.parsecodetree'
I found this from RealPython.com more confusing than helpful. If __init__.py
is not required, why do I even have this issue to begin with?
https://realpython.com/lessons/package-initialization/
Note: Much of the Python documentation states that an
__init__.py
file
must be present in the package directory when creating a package. This
was once true. It used to be that the very presence of__init__.py
signified to Python that a package was being defined. The file could
contain initialization code or even be empty, but it had to be
present.Starting with Python 3.3, Implicit Namespace Packages were introduced.
These allow for the creation of a package without any__init__.py
file. Of course, it can still be present if package initialization is
needed. But it is no longer required.
I saw a lot of stuff about setting the __package__
variable. But where do I put this assignment? I tried settings. That didn’t work.
I don’t understand the mapping from python “package” to django “Project”
BASE_DIR == “Project”, but are both also == “package” or “__package__
”?
In my setup, are both parsecodetree and ustitles “packages”? Whether I set them as == __package__
or not? Whether they have __init__.py
or not?
This SO post makes it seem like every directory needs an __init__.py
, just the opposite of the RealPython lesson. It doesn't seem DRY, either.
This gets red squiggly lines:
from ustitles.models import Statute
and a traceback to boot:
This is ustitles.__init__
Traceback (most recent call last):
<...snip…>
ModuleNotFoundError: No module named 'parsecodetree.parsecodetree'
But do you notice? Now “This is ustitles.__init__
” prints!
Along the same lines, how do you run “-m” with PyCharm “run”? Turns out this is still an open question: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003879119-how-to-run-python-m-command-in-pycharm-?page=1#community_comment_5925736517010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论