[英]How to organize python files, so that importing does not give ImportError:?
This question has been asked a million times.这个问题已经被问了一百万次了。 However none of the answers seems to be able to solve my problem.但是,似乎没有一个答案能够解决我的问题。
I have a python module, with some scripts embedded in a c++ project.我有一个 python 模块,在 c++ 项目中嵌入了一些脚本。 I have the following folder structure:我有以下文件夹结构:
mcmpy/
setup.py
doc/
mcm/
__init__.py
cpp/
__init__.py
kindyn.py
demos/
__init__.py
importer.py
in importer.py I tried, and does not work:在 importer.py 我试过了,但不起作用:
What I want to achieve is to have some scripts in demo folder, and be able to run them from terminal with python3 script_name .py我想要实现的是在演示文件夹中有一些脚本,并且能够使用 python3 script_name .py 从终端运行它们
You need to add a __init__.py
file inside mcmpy/您需要在 mcmpy/ 中添加一个__init__.py
文件
mcmpy/
__init__.py
setup.py
doc/
mcm/
__init__.py
cpp/
__init__.py
kindyn.py
demos/
__init__.py
importer.py
And inside the mcmpy folder, you just need to在 mcmpy 文件夹中,您只需要
from mcm.cpp import kindyn
To import with from mcmpy.mcm.cpp import kindyn
you will need to install this project - pip install -e <path of mcmpy>
.要使用from mcmpy.mcm.cpp import kindyn
,您需要安装此项目 - pip install -e <path of mcmpy>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.