了解本地运行与从PyPI安装时导入的差异

时间:2019-03-11 12:03:02

标签: python python-import pypi python-packaging

我创建了一个具有以下结构的软件包:

/data_worker/
    .git/
    data_worker/
        wrappers/
            alpha_vantage_wrapper.py
            quandl_wrapper.py
            wrapper.py
        __init__.py
        data_worker.py
        defined_wrappers.py
        mapper.py
    tests/
        test.py
    .gitignore
    LICENSE
    README.md
    requirements.txt
    setup.py
    test.py

/data_worker/data_worker/data_worker.py以以下几行开头:

from defined_wrappers import wrappers
import mapper

import pandas as pd
import json
import os

DATA_WORKER_DIR = os.path.dirname(os.path.abspath(__file__)) + '/'


class DataWorker:
    ...

/tests/test.py中,我正在测试从一个完全独立的目录运行该软件包,因为一旦我pip install该软件包便会执行此操作。 我有以下内容:

from data_worker.data_worker import DataWorker

dw = DataWorker()

运行正常,但出现以下错误:

Traceback (most recent call last):
  File "/home/kevin/Documents/data_worker/tests/test.py", line 1, in <module>
    from data_worker.data_worker import DataWorker
  File "/usr/local/lib/python3.6/dist-packages/data_worker/data_worker.py", line 1, in <module>
    from defined_wrappers import wrappers
ModuleNotFoundError: No module named 'defined_wrappers'

所以我的问题是:

如何构造项目的导入,以便从PyPI(或从我的tests目录安装后)可以无错运行?

0 个答案:

没有答案