无法执行包中的模块

Cannot execute module in a package

我绝对是 Python 初学者 :)

Python 3.6.4

结构:

python-packaging-dependencies
    dist
        python-packaging-dependencies-1.0.tar.gz
    jsonops
        pyjo_demo.py
        pyjo_spec.py
        __init__.py
        __pycache__
    LICENSE.txt
    MANIFEST.in
    python_packaging_dependencies.egg-info
    README.rst
    setup.cfg
    setup.py
    __init__.py

root init.py:

from jsonops import *

jsonops/init.py:

__all__ = ['pyjo_demo','pyjo_spec'] 

pyjo_spec.py

from pyjo import Model, Field, RangeField, EnumField
from enum import Enum

class Gender(Enum):
    female = 0
    male = 1

class Address(Model):
    city = Field(type=str)
    postal_code = Field(type=int)
    address = Field()

class User(Model):
    name = Field(type=str, repr=True, required=True)
    age = RangeField(min=18, max=120)
    #  equivalent to: Field(type=int, validator=lambda x: 18 <= x <= 120)
    gender = EnumField(enum=Gender)
    address = Field(type=Address)

pyjo_demo.py

from pyjo_spec import Gender, Address, User
def to_dictionary():
    u = User(name='john', age=18, address=Address(city='NYC'))
    print(u.to_dict())
    # {
    #     "name": "john",
    #     "age": 18,
    #     "address": {
    #         "city": "NYC"
    #     }
    # }

def from_dictionary():
    u = User.from_dict({
        "name": "john",
        "gender": "male",
        "age": 18,
        "address": {
            "city": "NYC"
        }
    })

    print(u)
    # <User(name=john)>

    print(u.gender)
    # Gender.male

    print(u.address.city)
    # NYC

在没有打包的情况下工作正常 - 我导航到目录并调用模块 pyjo_demo

E:\python-packaging-dependencies\jsonops>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyjo_demo
>>>
>>> pyjo_demo.to_dictionary()
{'name': 'john', 'age': 18, 'address': {'city': 'NYC'}}
>>> pyjo_demo.from_dictionary()
<User(name=john)>
Gender.male
NYC
>>>
>>>

现在我创建一个包并手动安装它。软件包已成功安装并列出:

E:\python-packaging-dependencies>pip install dist\python-packaging-dependencies-1.0.tar.gz
Processing e:\python-packaging-dependencies\dist\python-packaging-dependencies-1.0.tar.gz
Collecting pyjo (from python-packaging-dependencies==1.0)
  Using cached pyjo-2.0.0.tar.gz
Requirement already satisfied: six in e:\development\software\environments
\python3.6.4\lib\site-packages (from pyjo->python-packaging-dependencies==1.0)

Installing collected packages: pyjo, python-packaging-dependencies
  Running setup.py install for pyjo ... done
  Running setup.py install for python-packaging-dependencies ... done
Successfully installed pyjo-2.0.0 python-packaging-dependencies-1.0

E:\python-packaging-dependencies>pip freeze --local
certifi==2018.1.18
chardet==3.0.4
click==6.7
idna==2.6
pkginfo==1.4.1
pyjo==2.0.0
python-packaging-dependencies==1.0
requests==2.18.4
requests-toolbelt==0.8.0
six==1.11.0
tqdm==4.19.5
twine==1.9.1
urllib3==1.22

尽管我不确定 init.py 中的条目是否正确,但如果我手动执行所有导入,代码应该可以工作:

    Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
     on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>> import jsonops
    >>>
    >>> pyjo_demo.to_dictionary()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'pyjo_demo' is not defined
    >>>
    >>> import pyjo_demo
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'pyjo_demo'
    >>>
    >>> from jsonops import pyjo_demo
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "E:\Development\Software\Environments\Python3.6.4\lib\site-packag
    es\jsonops\pyjo_demo.py", line 1, in <module>
        from pyjo_spec import Gender, Address, User
    ModuleNotFoundError: No module named 'pyjo_spec'
    >>>
    >>> import jsonops.pyjo_spec
>>> import jsonops.pyjo_demo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\Development\Software\Environments\Python3.6.4\lib\site-packag
es\jsonops\pyjo_demo.py", line 1, in <module>
    from pyjo_spec import Gender, Address, User
ModuleNotFoundError: No module named 'pyjo_spec'

我找不到什么错误?

************编辑-1************ 根据 Ghilas BELHADJ 的评论,将子目录的名称从 'json' 更改为 'json-custom'。现在,我根本无法导入任何东西。

************编辑 2 ********** 将子目录的名称从 'json-custom' 更改为 'jsonops'。同样的错误

通过导入 jsonops,您并没有导入包中的文件。 你应该使用 from jsonops import pyjo_demo.

运行 代码会给出另一个错误,因为 pyjo_spec 不是模块:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/PycharmProjects/test/jsonops/pyjo_demo.py", line 1, in <module>
    from pyjo_spec import Gender, Address, User
ModuleNotFoundError: No module named 'pyjo_spec'

要修复此错误,您可以将 pyjo_demo.py 中的导入更改为 import jsonops.pyjo_spec

pyjo_demo.py 则变为:

from jsonops.pyjo_spec import Gender, Address, User
def to_dictionary():
    u = User(name='john', age=18, address=Address(city='NYC'))
    print(u.to_dict())
    # {
    #     "name": "john",
    #     "age": 18,
    #     "address": {
    #         "city": "NYC"
    #     }
    # }

def from_dictionary():
    u = User.from_dict({
        "name": "john",
        "gender": "male",
        "age": 18,
        "address": {
            "city": "NYC"
        }
    })

    print(u)
    # <User(name=john)>

    print(u.gender)
    # Gender.male

    print(u.address.city)
    # NYC