Importing Python package - "ImportError: No module named..."
Importing Python package - "ImportError: No module named..."
我知道有很多关于 "ImportError: No module named..." 的问题,但它们通常似乎归结为没有 __init__.py
文件或包目录不在 $PYTHONPATH
中。我已经检查了这两个问题,我的问题不在于它们。
我有一个包含协议缓冲区定义的项目。有一个 makefile 将源代码生成为 Python、Java 或 Go。有一个 setup.py
文件执行 make python
。我在这个目录中 运行 pip install -e .
按预期生成源文件。
然后我有一个单独的项目,我在其中尝试使用生成的 protobuf。
让我说明一下我的项目:
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
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,
}
)
myprotos
中的__init__.py
只包含:
import my_pb2
然后myproject/main.py
的内容是:
import sys
sys.path.insert(0, '/path/to/myprotos')
import myprotos
运行 这段代码,python main.py
输出:
Traceback (most recent call last):
File "main.py", line 12, in <module>
import myprotos
ImportError: No module named myprotos
我错过了什么?看起来这应该可行,但我显然还没有理解一些关键的东西。
假设您有以下结构:
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
main.py中的代码:
import sys
sys.path.insert(0, '/path/to/demo_proj')
import myprotos
我知道有很多关于 "ImportError: No module named..." 的问题,但它们通常似乎归结为没有 __init__.py
文件或包目录不在 $PYTHONPATH
中。我已经检查了这两个问题,我的问题不在于它们。
我有一个包含协议缓冲区定义的项目。有一个 makefile 将源代码生成为 Python、Java 或 Go。有一个 setup.py
文件执行 make python
。我在这个目录中 运行 pip install -e .
按预期生成源文件。
然后我有一个单独的项目,我在其中尝试使用生成的 protobuf。
让我说明一下我的项目:
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
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,
}
)
myprotos
中的__init__.py
只包含:
import my_pb2
然后myproject/main.py
的内容是:
import sys
sys.path.insert(0, '/path/to/myprotos')
import myprotos
运行 这段代码,python main.py
输出:
Traceback (most recent call last):
File "main.py", line 12, in <module>
import myprotos
ImportError: No module named myprotos
我错过了什么?看起来这应该可行,但我显然还没有理解一些关键的东西。
假设您有以下结构:
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
main.py中的代码:
import sys
sys.path.insert(0, '/path/to/demo_proj')
import myprotos