这是我删除的非代码内容的精简版setup.py脚本:
#!/usr/bin/env python
from distutils.core import setup
from whyteboard.misc import meta
setup(
name = 'Whyteboard',
version = meta.version,
packages = ['whyteboard', 'whyteboard.gui', 'whyteboard.lib', 'whyteboard.lib.pubsub',
'whyteboard.lib.pubsub.core', 'whyteboard.lib.pubsub.utils', 'whyteboard.misc'],
py_modules = ['whyteboard'],
scripts = ['whyteboard.py'],
)
Run Code Online (Sandbox Code Playgroud)
MANIFEST.in:
include *.txt
include whyteboard-help/*.*
recursive-include locale *.mo
recursive-include images *.png
Run Code Online (Sandbox Code Playgroud)
当我运行"python setup.py install sdist"时,我得到一个很好的.tar.gz,带有"whyteboard-0.41"根文件夹,里面有我的locale/images /和whyteboard-help/folders.这也有我的whyteboard.py脚本,它从whyteboard源包中启动我的程序.
所以:
whyteboard/
??? locale/
??? images
??? whyteboard-help/
??? whyteboard/
? ??? __init__.py
? ??? other packages etc
??? whyteboard.py
??? README
??? setup.py
??? CHANGELOG
Run Code Online (Sandbox Code Playgroud)
这反映了我的程序的来源,一切应该是怎样的,并且是正确的.
但是,当我运行"python setup.py install"时,我的数据文件都没有写入 …
你能告诉我如何读取Python包中的文件?
我加载的包有许多我想从程序中加载的模板(用作字符串的文本文件).但是如何指定此类文件的路径?
想象一下,我想从以下位置读取文件:
package\templates\temp_file
Run Code Online (Sandbox Code Playgroud)
某种路径操纵?包基路径跟踪?
我有以下文件结构:
test/
test1.py
test2.py
text.txt
Run Code Online (Sandbox Code Playgroud)
以下是文件的内容
test1.py:
import sys
sys.path.append('../')
import test2
test2.read()
Run Code Online (Sandbox Code Playgroud)
test2.py:
def read():
with open('text.txt', 'rb') as f:
print f.read()
if __name__ == "__main__":
read()
Run Code Online (Sandbox Code Playgroud)
text.txt包含一行文字.当我运行时test1.py,我收到"找不到文件"错误:
Traceback (most recent call last):
File "test1.py", line 5, in <module>
test2.read()
File "../test2.py", line 2, in read
with open('text.txt', 'rb') as f:
IOError: [Errno 2] No such file or directory: 'text.txt'
Run Code Online (Sandbox Code Playgroud)
我有点理解为什么会出现这个错误.但是我该如何处理这些错误呢.我希望代码test2.py就像我可以在任何地方使用的库代码.