简体   繁体   English

导入Python包-“ ImportError:未命名模块...”

[英]Importing Python package - “ImportError: No module named…”

I know there are a lot of questions about "ImportError: No module named..." but they ususally seem to boil down to no __init__.py file or the package directory not in $PYTHONPATH . 我知道关于“ ImportError:未命名的模块...”有很多问题,但是它们通常似乎可以归结为无__init__.py文件或$PYTHONPATH没有的软件包目录。 I've checked both of those issues and my issue is not down to them. 我已经检查了这两个问题,但我的问题还不止于这些。

I have a project which contains protocol buffer definitions. 我有一个包含协议缓冲区定义的项目。 There's a makefile which generates the source as Python, Java or Go. 有一个生成文件,其生成源为Python,Java或Go。 There's a setup.py file which executes make python . 有一个setup.py文件,它执行make python I've run pip install -e . 我已经运行了pip install -e . in this directory which generates the source files as expected. 在此目录中,该目录将按预期生成源文件。

I then have a separate project where I'm trying to use the generated protobufs. 然后,我有一个单独的项目,尝试使用生成的protobuf。

Let me illustrate my projects: 让我说明一下我的项目:

myproject/
├── module
│   ├── __init__.py
│   └── module.py
└── main.py

myprotos/
├── Makefile
├── __init__.py
├── my.proto
├── my_pb2.py (generated by the makefile on install)
├── myprotos.egg-info (generated by setup.py)
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   └── top_level.txt
└── setup.py

The source of setup.py is pretty simple: setup.py的源非常简单:

import subprocess
import sys

from setuptools import setup
from setuptools.command.install import install

class Install(install):
    """Customized setuptools install command - builds protos on install."""
    def run(self):
        protoc_command = ["make", "python"]
        if subprocess.call(protoc_command) != 0:
            sys.exit(-1)
        install.run(self)


setup(
    name='myprotos',
    version='0.0.1',
    description='',
    install_requires=[],
    cmdclass={
        'install': Install,
    }
)

The __init__.py in myprotos simply contains: myprotos__init__.py仅包含:

import my_pb2

And then the contents of myproject/main.py is: 然后myproject/main.py的内容是:

import sys
sys.path.insert(0, '/path/to/myprotos')

import myprotos

Running this code, python main.py outputs: 运行以下代码, python main.py输出:

Traceback (most recent call last):
  File "main.py", line 12, in <module>
    import myprotos
ImportError: No module named myprotos

What have I missed here? 我在这里错过了什么? It seems like this should work but I clearly haven't understood something crucial. 看来这应该可行,但是我显然还不了解一些关键的内容。

Let say you have below structure : 假设您具有以下结构:

demo_proj
    |
    myproject/
    ├── module
    │   ├── __init__.py
    │   └── module.py
    └── main.py

    myprotos/
    ├── Makefile
    ├── __init__.py
    ├── my.proto
    ├── my_pb2.py
    ├── myprotos.egg-info
    │   ├── PKG-INFO
    │   ├── SOURCES.txt
    │   ├── dependency_links.txt
    │   └── top_level.txt
    └── setup.py

Code in main.py: main.py中的代码:

import sys
sys.path.insert(0, '/path/to/demo_proj')

import myprotos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python ImportError:没有名为包的模块 - python ImportError: No module named package 将python包导入python模块时ImportError - ImportError while importing a python package into a python module ImportError:没有名为package的模块 - ImportError: No module named package ImportError:导入我自己的子包时没有名为“”的模块 - ImportError: No module named '' when importing my own sub-package “ ImportError:没有名为xhaus的模块” Python模块从ansible导入错误 - “ImportError: No module named xhaus” Python module importing error from ansible 跨包导入模块时,Python“ ImportError:未命名模块” - Python 'ImportError: No module named' when importing modules across packages mod_python和子包导入问题:ImportError:未命名模块 - mod_python and subpackages importing issues: ImportError: No module named Python-导入paypalrestsdk导致ImportError:没有名为_winreg的模块 - Python - Importing paypalrestsdk causes ImportError: No module named _winreg 导入Pocketsphinx无法在python 3.2(Windows)中运行。 “ ImportError:没有名为pocketsphinx的模块” - Importing pocketsphinx not working python 3.2 (Windows). “ImportError: No module named pocketsphinx” python导入模块ImportError - python importing module ImportError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM