创建包含多个 python 个文件的包
Creating a package with several python files
我有一个文件夹结构:
setup_seplot.py
seplot/
__init__.py (empty)
seplot.py
kw_dictionaries.py
在 seplot.py 中,我有:
import kw_dictionaries as kd
如果我运行seplot.py,一切正常。
但是我使用setup_seplot.py的时候出现了问题:
python setup_seplot.py sdist bdist_wheel
Traceback (most recent call last):
File "setup_seplot.py", line 2, in <module>
from seplot import seplot as sep
File "/home/XXXXX/code/Python-Tools/seplot/seplot.py", line 14, in <module>
import kw_dictionaries as kd
ModuleNotFoundError: No module named 'kw_dictionaries'
这个问题似乎是因为在 setup_seplot 中,我导入了 seplot 来获取版本:
设置_seplot.py:
from setuptools import setup, Extension, find_packages
from seplot import seplot as sep
version=sep.__VERSION__
setup(
name='seplot',
version=version,
description="A front-end for Python PyX",
install_requires=[ 'pyx', ],
packages=find_packages(),
scripts=['seplot/bin/seplot','seplot/seplot.py',
'seplot/kw_dictionaries.py','seplot/style_dictionaries.py']
)
如果在seplot.py我替换
import kw_dictionaries as kd
来自:
from . import kw_dictionaries as kd
然后设置工作正常,但代码 (setup.py) 没有。
我在这里迷路了。
解决方案位于:another topic as suggested by Gocht。
感觉有点乱,我似乎不明白为什么,但这行得通:
if __package__:
import seplot.kw_dictionaries as kd
else:
import kw_dictionaries as kd
- 通过使用 旧式
scripts
from distutils. Or even better, use the console_scripts
entry points from setuptools.
- 始终在可执行脚本中使用绝对导入(如果有的话)。
- 根据您的喜好在包中使用绝对或相对导入(我相信绝对在长 运行 中更好)。
- 在设置脚本中,不要从项目中导入包。将版本字符串保留在
setup.cfg
(或 setup.py
)中并使用 importlib_metadata.version('ProjectName')
从您的实际代码中读取。
我有一个文件夹结构:
setup_seplot.py
seplot/
__init__.py (empty)
seplot.py
kw_dictionaries.py
在 seplot.py 中,我有:
import kw_dictionaries as kd
如果我运行seplot.py,一切正常。
但是我使用setup_seplot.py的时候出现了问题:
python setup_seplot.py sdist bdist_wheel
Traceback (most recent call last):
File "setup_seplot.py", line 2, in <module>
from seplot import seplot as sep
File "/home/XXXXX/code/Python-Tools/seplot/seplot.py", line 14, in <module>
import kw_dictionaries as kd
ModuleNotFoundError: No module named 'kw_dictionaries'
这个问题似乎是因为在 setup_seplot 中,我导入了 seplot 来获取版本:
设置_seplot.py:
from setuptools import setup, Extension, find_packages
from seplot import seplot as sep
version=sep.__VERSION__
setup(
name='seplot',
version=version,
description="A front-end for Python PyX",
install_requires=[ 'pyx', ],
packages=find_packages(),
scripts=['seplot/bin/seplot','seplot/seplot.py',
'seplot/kw_dictionaries.py','seplot/style_dictionaries.py']
)
如果在seplot.py我替换
import kw_dictionaries as kd
来自:
from . import kw_dictionaries as kd
然后设置工作正常,但代码 (setup.py) 没有。 我在这里迷路了。
解决方案位于:another topic as suggested by Gocht。
感觉有点乱,我似乎不明白为什么,但这行得通:
if __package__:
import seplot.kw_dictionaries as kd
else:
import kw_dictionaries as kd
- 通过使用 旧式
scripts
from distutils. Or even better, use theconsole_scripts
entry points from setuptools. - 始终在可执行脚本中使用绝对导入(如果有的话)。
- 根据您的喜好在包中使用绝对或相对导入(我相信绝对在长 运行 中更好)。
- 在设置脚本中,不要从项目中导入包。将版本字符串保留在
setup.cfg
(或setup.py
)中并使用importlib_metadata.version('ProjectName')
从您的实际代码中读取。