【发布时间】:2020-02-09 00:19:53
【问题描述】:
我的项目结构如下:
project/
+ setup.py
+ bobafett/
+ __init__.py
+ __main__.py
+ foo.py
与:
-
__init__.py: 一个空文件 -
foo.py:包含bar的定义:
def bar():
print("foo.bar()")
-
__main__.py导入使用foo.bar:
import foo
foo.bar()
✓ 在本地,一切正常:
~/project $ python3 bobafett/__main__.py
foo.bar()
✗ 一旦 bobafett 被打包、发布和安装,foo 就找不到了
~/project $ python3 setup.py bdist_wheel
~/project $ curl -T ... https://my-pypi.example.com/simple/bobafett/...
~/project $ cd /tmp
/tmp $ pip --index-url https://my-pypi.example.com/simple --user bobafett
/tmp $ python -m bobafett
...
File "/home/ysc/.local/lib/python3.6/site-packages/bobafett/__main__.py", line 1, in <module>
import foo
ModuleNotFoundError: No module named 'foo'
怎么样?我可以写什么来代替import foo 既可以在本地工作又可以在部署后使用?我需要改变我的项目结构吗?
setup.py:
from setuptools import setup
setup(
name='bobafett',
version='1.0.0',
author='YSC',
author_email='ysc@example.com',
packages=['bobafett'],
url='https:///my-pypi.example.com/simple/bobafett/',
description='Prints "foo.bar()".',
)
【问题讨论】:
标签: python python-3.x module package