OSXでPython 2.6.1を実行すると、CentOSにデプロイされます。このようなコマンドラインから起動するパッケージがしたい:そのためにPythonパッケージを実行する
python [-m] tst
を、ここで作られたディレクトリ構造である:
$PYTHONPATH/
tst/
__init__.py # empty
__main__.py # below
dep.py # below
次はファイルである:
$ cat tst/__main__.py
from .dep import DepClass
print "Hello there"
$ cat tst/dep.py
class DepClass(object):
pass
$
しかし、Pythonは診断矛盾する私を与える:
$ python -m tst
/usr/bin/python: tst is a package and cannot be directly executed
OK、パッケージとして認識されます。だから私はスクリプトとしてそれを実行できるはずですか?それは私が失われています。この時点で__main__
...
$ python tst
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 121, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 34, in _run_code
exec code in run_globals
File "/Users/vdidenko/Code/emi/tst/__main__.py", line 1, in <module>
from .dep import DepClass
ValueError: Attempted relative import in non-package
を持っています。なぜnon-package
?そして、コードをどのように構造化するのですか?
dupではなく、[-m]、別の解決方法で実行したときの応答が異なります。しかし、近くに音がします。この問題は、実行時に '.__ main__'を追加できませんでした。もう1つは正しく追加しました。 –