Python C使用相对导入配置模块

Python C使用相对导入配置模块,python,import,cprofile,Python,Import,Cprofile,我在mymodule mymodule ├── config.py ├── __init__.py └── lib.py 通过以下简单内容: # config.py NAME = "Julius Cesar" # lib.py from .config import NAME def get_name(): return NAME 我可以使用python-m mymodule.lib运行它(并且不会发生任何事情) 但我不能描述它: » python -m cProfile mym

我在
mymodule

mymodule
├── config.py
├── __init__.py
└── lib.py
通过以下简单内容:

# config.py
NAME = "Julius Cesar"

# lib.py
from .config import NAME

def get_name():
    return NAME
我可以使用
python-m mymodule.lib运行它(并且不会发生任何事情)

但我不能描述它:

» python -m cProfile mymodule/lib.py 
         2 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 lib.py:1(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/lib/python2.7/cProfile.py", line 199, in <module>
    main()
  File "/usr/lib/python2.7/cProfile.py", line 192, in main
    runctx(code, globs, None, options.outfile, options.sort)
  File "/usr/lib/python2.7/cProfile.py", line 49, in runctx
    prof = prof.runctx(statement, globals, locals)
  File "/usr/lib/python2.7/cProfile.py", line 140, in runctx
    exec cmd in globals, locals
  File "mymodule/lib.py", line 1, in <module>
    from .config import NAME
ValueError: Attempted relative import in non-package
»python-mcprofile mymodule/lib.py
在0.000秒内进行2次函数调用
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
1 0.000 0.000 0.000 0.000 lib.py:1()
1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/runpy.py”,第162行,在运行模块中作为主模块
“\uuuuu main\uuuuuuuuuuuuuuuuuuuuuuuuu”,fname,loader,pkg\u name)
文件“/usr/lib/python2.7/runpy.py”,第72行,在运行代码中
run_globals中的exec代码
文件“/usr/lib/python2.7/cProfile.py”,第199行,在
main()
文件“/usr/lib/python2.7/cProfile.py”,第192行,在main中
runctx(代码、全局、无、options.outfile、options.sort)
runctx中的文件“/usr/lib/python2.7/cProfile.py”,第49行
prof=prof.runctx(语句、全局、局部)
runctx中的文件“/usr/lib/python2.7/cProfile.py”,第140行
全局、局部中的exec cmd
文件“mymodule/lib.py”,第1行,在
from.config导入名称
ValueError:尝试在非包中进行相对导入
那么,我怎样才能
cProfile
a库呢?因为库没有做任何事情,所以只分析lib模块的导入,但这对我来说已经足够好了。我不想在这个阶段分析所有函数调用,只想导入模块


对于具有相对导入的模块,如何使用cProfile实现这一点,避免出现
ValueError
s?

我希望我的回答是正确的,但是您可以像这样使用cProfile API:

» python
>>> import cProfile
>>> cProfile.run('import mylib.lib')
  511 function calls (500 primitive calls) in 0.002 seconds
  Ordered by: standard name
  ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   3    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:119(release)
   ...
>>>
»python
>>>导入cProfile
>>>cProfile.run('import mylib.lib')
0.002秒内511个函数调用(500个基本调用)
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
3 0.000 0.000 0.000 0.000:119(发布)
...
>>>
或者您也可以在一行中完成,比如
python-c“import cProfile;cProfile.run('import mylib.lib')”

希望这有帮助