运行python包
在OSX上运行Python 2.6.1将部署到CentOS。希望从以下命令行调用包:运行python包,python,module,package,main,python-2.6,Python,Module,Package,Main,Python 2.6,在OSX上运行Python 2.6.1将部署到CentOS。希望从以下命令行调用包: python [-m] tst 为此,以下是制作的目录结构: $PYTHONPATH/ tst/ __init__.py # empty __main__.py # below dep.py # below 文件中包含以下内容: $ cat tst/__main__.py from .dep import DepCla
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
好的,所以它被认为是一个包。所以我应该能够以脚本的形式运行它?它有\uuuu main\uuuu
$ 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
$pythontst
回溯(最近一次呼叫最后一次):
文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py”,第121行,在运行模块中
“\uuuuu main\uuuuuuuuuuuuuuuuuuuuuuuuu”,fname,loader,pkg\u name)
文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py”,第34行,在运行代码中
run_globals中的exec代码
文件“/Users/vdidenko/Code/emi/tst/_____.py”,第1行,在
from.dep导入DepClass
ValueError:尝试在非包中进行相对导入
在这一点上我迷路了。为什么
非包装
?那么如何构造代码呢?Python 2.7中引入了在使用命令行-m
选项时运行包的\uuuu main\uuu
模块的功能。对于2.6,您需要指定要运行的包模块名称<代码>-m测试。主\uuuuuuuuuuu应该可以工作。请参阅文档。太好了,谢谢!2.6x中的文档在不同版本之间有点模糊和混乱,没有帮助。//,这也是python-m pip install argparse等语句的问题。不是dup,使用[-m]运行时有不同的响应,不同的解决方案。尽管听起来很接近。此问题无法在运行时添加。\uuuuu main\uuuuu
,另一个问题确实正确添加了它。