在Python中导入时未解析的参考

时间:2019-02-04 14:11:57

标签: python python-3.x pip python-import setup.py

我已经使用Pip从GitHub安装了Python软件包。它将其安装到我包中的src文件夹中。我的文件夹结构现在看起来像这样:

TestApiClient/src/sdk-config/config_a

我需要导入ConfigurationApiManager

我尝试了这个,但是不起作用:

from src.config_a.configuration_api import ConfigurationApi
from src.config_a.manager import Manager

应如何导入?

ConfigurationApi setup.py

#!/usr/bin/env python
import os
import re
import sys

from setuptools import setup, find_packages

# Config sdk python version check
_valid  =  sys.version_info[:2] == (2, 7) or sys.version_info >= (3,4)
if not _valid:
    sys.exit("Sorry, SDK only supports versions 2.7, 3.4, 3.5, 3.6, 3.7 of python.")


ROOT = os.path.dirname(__file__)
VERSION_RE = re.compile(r'''__version__ = ['"]([a-z0-9.]+)['"]''')


def get_version():
    init = open(os.path.join(ROOT, 'config', '__init__.py')).read()
    return VERSION_RE.search(init).group(1)


setup(
    name='sdk-config',
    version=get_version(),
    description='Config SDK for Python',
    long_description=open('README.md').read(),
    author='ME',
    url='https://github.my_company.com/my_org/repo_name',
    scripts=[],
    packages=find_packages(exclude=['tests*']),
    include_package_data=True,
    license="Apache License 2.0",
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
    ],
)

其他信息

我已卸载并重新安装,但是这次我没有以可编辑模式安装。

如果执行pip list,我可以看到sdk-config已安装。我尝试使用sdk_config = __import__("sdk-config")导入该文件,以为我可以做类似的事情:

from sdk_config.config_a.configuration_api import ConfigurationApi
from sdk_config.config_a.manager import Manager

但是,导致此错误:

Traceback (most recent call last):
  File "/Users/me/PycharmProjects/TestConfigApiClient/TestApiClient.py", line 1, in <module>
    sdk_config = __import__("sdk-config")
ModuleNotFoundError: No module named 'sdk-config'

1 个答案:

答案 0 :(得分:0)

问题似乎是我在全球安装了该软件包,但是我正在一个没有该软件包的虚拟环境中工作。然后将其安装在虚拟环境中时,它可以工作。