相关疑难解决方法(0)

__file__变量是什么意思/做什么?

A = os.path.join(os.path.dirname(__file__), '..')

B = os.path.dirname(os.path.realpath(__file__))

C = os.path.abspath(os.path.dirname(__file__))
Run Code Online (Sandbox Code Playgroud)

我通常只是用实际路径来硬连线.但是这些语句在运行时确定路径是有原因的,我真的很想了解os.path模块,所以我可以开始使用它.

python

140
推荐指数
6
解决办法
15万
查看次数

如何确定文件,功能和行号?

在C++中,我可以像这样打印调试输出:

printf(
   "FILE: %s, FUNC: %s, LINE: %d, LOG: %s\n",
   __FILE__,
   __FUNCTION__,
   __LINE__,
   logmessage
);
Run Code Online (Sandbox Code Playgroud)

我怎样才能在Python中做类似的事情?

python

53
推荐指数
6
解决办法
3万
查看次数

如何使用structlog添加代码行号

使用 python 标准日志模块,可以使用以下方法添加原始日志调用的行号: %(lineno)s.

这如何使用 structlog 来完成?

structlog

9
推荐指数
3
解决办法
1107
查看次数

Python - Get the source code of the line that called me

Using the python inspect module, in a function, I would like to get the source code of the line that called that function.

So in the following situation:

def fct1():
    # Retrieve the line that called me and extract 'a'
    return an object containing name='a'

a = fct1()
Run Code Online (Sandbox Code Playgroud)

I would like to retrieve the string "a = fct1()" in fct1

All I can do so far is to retrieve the code of the whole module with :

code = inspect.getsource(sys._getframe().f_back) …
Run Code Online (Sandbox Code Playgroud)

python inspect

5
推荐指数
1
解决办法
338
查看次数

相当于C ++的Python __LINE__

在C ++中进行调试的有用打印内容是

std::cout << __LINE__ << std::endl;
Run Code Online (Sandbox Code Playgroud)

当然,您可以简单地打印带有行号的字符串,例如:

std::cout << "this is line 54" << std::endl;
Run Code Online (Sandbox Code Playgroud)

但是当您四处移动时,它不会一直更改行号。Python中是否有任何等效的宏?

python

3
推荐指数
3
解决办法
88
查看次数

Python - 打印() - 调试 - 显示文件和行号

在python中使用print()时,是否可以打印它被调用的地方?所以输出看起来像 php 中的 var_dump 和 xdebug。例如。我有脚本 D:\Something\script.py,在第 50 行,有一个打印(“sometest”),所以输出看起来像这样:

D:\Somethinq\script.py:50 sometest
Run Code Online (Sandbox Code Playgroud)

或者是否有任何模块可以实现这一点?在大型项目中,很难管理这些印刷品的来源。

python printing debugging

2
推荐指数
1
解决办法
5179
查看次数

标签 统计

python ×5

debugging ×1

inspect ×1

printing ×1

structlog ×1