如何在python中加载带有参数的python模块?
我想创建一个python模块,该模块将使用如何在python中加载带有参数的python模块?,python,module,python-module,argv,Python,Module,Python Module,Argv,我想创建一个python模块,该模块将使用python-m mymodule somefile.py some_arg some_arg调用 我的想法是,我可以设置一个别名alias=“python-m mymodule”,并使用python somefile.py some\u arg some\u arg正常调用文件 在文件mymodule/\uuuuu main\uuuuuu.py中,加载somefile.py并将参数列表传递给它的最佳方式是什么 我正在寻找一个通用的解决方案,这将是py
python-m mymodule somefile.py some_arg some_arg
调用
我的想法是,我可以设置一个别名alias=“python-m mymodule”
,并使用python somefile.py some\u arg some\u arg
正常调用文件
在文件mymodule/\uuuuu main\uuuuuu.py
中,加载somefile.py
并将参数列表传递给它的最佳方式是什么
- 我正在寻找一个通用的解决方案,这将是python2和3兼容
- 最好尽量少打扰。如果
会引发异常,则在回溯中几乎看不到somefile.py
mymodule
- 模块所做的细节在这里并不有趣,但它设置了一些python东西(回溯挂钩等),因此
应该在相同的过程中以pythonicly方式运行<代码>操作系统或somefile.py
不适合子流程
$python3-m mymodule somefile.py some_arg some_arg
['somefile.py','some_arg','some_arg']
回溯(最近一次呼叫最后一次):
文件“somefile.py”,第3行,在
引发异常()
$python2-m mymodule somefile.py some_arg some_arg
['somefile.py','some_arg','some_arg']
回溯(最近一次呼叫最后一次):
文件“/usr/lib64/python3.5/runpy.py”,第184行,在“运行”模块中作为“主”
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“/usr/lib64/python3.5/runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“/home/azmeuk/dev/testpy/mymodule/\uuuuuu main\uuuuuuuu.py”,第16行,在
行政主任(代码)
文件“somefile.py”,第3行,在
引发异常()
$python somefile.py some\u arg some\u arg
['somefile.py','some_arg','some_arg']
回溯(最近一次呼叫最后一次):
文件“somefile.py”,第3行,在
引发异常()
例外情况
不过,如果有人有更好的建议,那就太好了 好的,我发现Python3.5有一些不错的东西,Python2.7也足够满足了 mymodule/main.py somefile.py 在候机楼
$python3-m mymodule somefile.py some_arg some_arg
['somefile.py','some_arg','some_arg']
回溯(最近一次呼叫最后一次):
文件“somefile.py”,第3行,在
引发异常()
$python2-m mymodule somefile.py some_arg some_arg
['somefile.py','some_arg','some_arg']
回溯(最近一次呼叫最后一次):
文件“/usr/lib64/python3.5/runpy.py”,第184行,在“运行”模块中作为“主”
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“/usr/lib64/python3.5/runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“/home/azmeuk/dev/testpy/mymodule/\uuuuuu main\uuuuuuuu.py”,第16行,在
行政主任(代码)
文件“somefile.py”,第3行,在
引发异常()
$python somefile.py some\u arg some\u arg
['somefile.py','some_arg','some_arg']
回溯(最近一次呼叫最后一次):
文件“somefile.py”,第3行,在
引发异常()
例外情况
不过,如果有人有更好的建议,那就太好了 我认为
limit
的负值在python 3.5之前的回溯模块中不起作用。下面是一个使用Python2.7的丑陋的hack
import sys
import traceback
class ExcFile(object):
def __init__(self, file):
self.topline = True
self.file = file
def write(self, s):
if self.topline:
u, s = s.split('\n', 1)
self.file.write(u +'\n')
self.topline = False
if '#---\n' in s:
u, s = s.split('#---\n', 1)
self.file.write(s)
self.write = self.file.write
ExcFile._instance = ExcFile(sys.stdout)
# The following block of code removes the part of
# the traceback related to this very module, and runpy
def myexcepthook(type, value, tb):
traceback.print_exception(type, value, tb, file=ExcFile._instance)
sys.excepthook = myexcepthook
if len(sys.argv) > 1:
file = sys.argv[1]
sys.argv = sys.argv[1:]
with open(file) as f:
code = compile(f.read(), file, 'exec')
exec(code) #---
所有这些都应该写在一个单独的文件中,以避免混乱
\uuu main\uuuuu.py
我认为limit
的负值在python 3.5之前的回溯模块中不起作用。下面是一个使用Python2.7的丑陋的hack
import sys
import traceback
class ExcFile(object):
def __init__(self, file):
self.topline = True
self.file = file
def write(self, s):
if self.topline:
u, s = s.split('\n', 1)
self.file.write(u +'\n')
self.topline = False
if '#---\n' in s:
u, s = s.split('#---\n', 1)
self.file.write(s)
self.write = self.file.write
ExcFile._instance = ExcFile(sys.stdout)
# The following block of code removes the part of
# the traceback related to this very module, and runpy
def myexcepthook(type, value, tb):
traceback.print_exception(type, value, tb, file=ExcFile._instance)
sys.excepthook = myexcepthook
if len(sys.argv) > 1:
file = sys.argv[1]
sys.argv = sys.argv[1:]
with open(file) as f:
code = compile(f.read(), file, 'exec')
exec(code) #---
所有这些都应该写在一个单独的文件中,以避免混乱
\uuuu main\uuuuu.py
这是一个很好的解决方案。确保在mymodule/\uuuuu main\uuuuuuuuuuuuuuupy
的全局字典中添加尽可能少的名称,因为somefile.py
不希望在其命名空间中包含这些对象。您还可以在traceback.print_tb
中使用limit
参数来显示干净的回溯。这是一个很好的解决方案。确保在mymodule/\uuuuu main\uuuuuuuuuuuuuuupy
的全局字典中添加尽可能少的名称,因为somefile.py
不希望在其命名空间中包含这些对象。您还可以在traceback.print_tb
中使用limit
参数来显示干净的回溯。
$ python3 -m mymodule somefile.py some_arg some_arg
['somefile.py', 'some_arg', 'some_arg']
Traceback (most recent call last):
File "somefile.py", line 3, in <module>
raise Exception()
$ python2 -m mymodule somefile.py some_arg some_arg
['somefile.py', 'some_arg', 'some_arg']
Traceback (most recent call last):
File "/usr/lib64/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib64/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/azmeuk/dev/testpy/mymodule/__main__.py", line 16, in <module>
exec(code)
File "somefile.py", line 3, in <module>
raise Exception()
$ python somefile.py some_arg some_arg
['somefile.py', 'some_arg', 'some_arg']
Traceback (most recent call last):
File "somefile.py", line 3, in <module>
raise Exception()
Exception
import sys
import traceback
class ExcFile(object):
def __init__(self, file):
self.topline = True
self.file = file
def write(self, s):
if self.topline:
u, s = s.split('\n', 1)
self.file.write(u +'\n')
self.topline = False
if '#---\n' in s:
u, s = s.split('#---\n', 1)
self.file.write(s)
self.write = self.file.write
ExcFile._instance = ExcFile(sys.stdout)
# The following block of code removes the part of
# the traceback related to this very module, and runpy
def myexcepthook(type, value, tb):
traceback.print_exception(type, value, tb, file=ExcFile._instance)
sys.excepthook = myexcepthook
if len(sys.argv) > 1:
file = sys.argv[1]
sys.argv = sys.argv[1:]
with open(file) as f:
code = compile(f.read(), file, 'exec')
exec(code) #---