无法在包中执行模块

时间:2018-02-15 11:03:05

标签: python python-3.x python-3.6 python-packaging

我是一个绝对的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

init .py:

from jsonops import *

jsonops /的初始化的.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\3.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\3.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\3.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的评论,改变了子目录的名称来自&#39; json&#39;到&#39; json-custom&#39;。现在,我根本无法导入任何东西。

**********编辑-2 ********** 从&#39; json-custom&#39;更改了子目录的名称。到了jsonops&#39;。同样的错误

1 个答案:

答案 0 :(得分:0)

通过导入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