首先,最好将需求安装到虚拟环境中,而不是系统Python中。值得注意的是,从Python3.3开始
venv
添加了模块,使此过程简单明了。
# globally load the module, let's check what's inside.
>>> import django
>>> dir(django) # only the content from the __init__.py was imported.
['VERSION', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__', '__version__',
'get_version', 'setup', 'utils']
>>> from django import *
>>> print(django.apps)
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'django' has no attribute 'apps'
>>> globals() # no Django submodules were loaded.
同时在Django shell中运行上述命令:
>>> import django
>>> dir(django)
['VERSION', '__builtins__','__cached__','apps','setup','template',
'templatetags','urls','utils', 'views', ...]
>>> print(django.apps)
>>> <module 'django.apps' from .../django/apps/__init__.py>
>>> globals() # some of the Django submodules were loaded
在控制台中进行全局Django导入时,来自
__init__.py
django
导入也使子模块可用。