简体   繁体   English

为什么运行python可执行文件后终端空白?

[英]Why is terminal blank after running python executable?

I added the hashbang line at the beginning of the python script ( #!/usr/bin/python ) and gave it executable privileges via chmod +x file.py command but after clicking on the module to run it, the terminal pops up but is blank. 我在python脚本( #!/usr/bin/python )的开头添加了hashbang行,并通过chmod +x file.py命令chmod +x file.py赋予了可执行特权,但是在单击模块以运行它后,终端弹出但是空白的。

Would it have something to do with the python interpreter possibly not being in the specified path? 它可能与不在指定路径中的python解释器有关吗?

It would appear that that is not a valid shebang . 看来这不是有效的射手

Try #!/usr/bin/python 尝试#!/usr/bin/python

(Note the exclaimation after the hash mark.) (请注意在井号后的惊叹号。)

Edit in response to comments from OP: 根据OP的评论进行编辑:

So, based on your comments about clicking the icon in a file manager and the platform being LXDE , it looks like in that use case it ends up calling into the g_app_info_create_from_commandline function from the GAppInfo library, which appears to be a whole layer for storing "application information and launch contexts". 因此,根据您对单击文件管理器中的图标以及平台为LXDE的评论 ,在该用例中,它似乎最终从GAppInfo库调用g_app_info_create_from_commandline函数,该函数似乎是存储“应用程序信息和启动上下文”。

Before that, it ends up bitwise-OR'ing in the G_APP_INFO_CREATE_NEEDS_TERMINAL flag into the flags field that it passes off to fm_app_info_create_from_commandline in the fm-file-launcher.c code . 在此之前,它最终在G_APP_INFO_CREATE_NEEDS_TERMINAL标志中按位或运算,进入flags字段,并传递给fm-file-launcher.c代码中的fm_app_info_create_from_commandline

So, it looks like double-clicking on the file is doing a ton of extra stuff before eventually running it in a terminal, and it would appear that that extra stuff (additional context, abstracted out via other libraries and layers, etc.) is what's causing the issue. 因此,双击该文件似乎会做大量的额外工作,然后才最终在终端中运行它,看来,这些额外的工作(附加上下文,通过其他库和层抽象出来的等)正在是什么引起了问题。

If the feature is really supposed to truly launch things in a terminal "normally", then I would regard it as a bug in the fm-file-launcher.c code or perhaps in the GAppInfo code itself. 如果该功能确实应该“正常”地在终端中真正启动,那么我将其视为fm-file-launcher.c代码或GAppInfo代码本身中的错误。

I'm not sure the developers would necessarily agree -- it will depend on what exactly is going on under the hood and what they intended that feature to be exactly. 我不确定开发人员是否一定会同意-这将取决于引擎盖下到底发生了什么,以及他们希望该功能到底是什么。 If you really want to be able to do this (I'm not sure I really get the benefit over simply using the ./file.py method directly in a terminal window), I would recommend filing a bug against the fm-file-launcher.c code. 如果您确实希望能够做到这一点(不确定在终端窗口中直接使用./file.py方法是否真的能带来好处),我建议您针对fm-file- launcher.c代码。 That is part of the libfm project, and it appears they don't track issues via their github page, but rather, their SourceForge page, according to their forum . 这是libfm项目的一部分,根据他们的论坛 ,他们似乎并没有通过github页面来跟踪问题,而是通过SourceForge页面来跟踪问题。

In short, double-clicking on the icon in the file manager and choosing to launch it in the terminal vs. actually launching it in a terminal are very, very different things, even if the former is meant to essentially function like the latter. 简而言之,双击文件管理器中的图标,然后选择在终端中启动它与在终端中实际启动它是非常非常不同的事情,即使前者在本质上要像后者那样工作。

As a side note, I tried doing this in Nautilus (I use Ubuntu ), and its behavior for that use case is to just automatically assume it's a text file and not an executable and load it into my default text editor. 附带说明一下,我尝试在Nautilus (我使用Ubuntu )中进行此操作,在该用例中,其行为是仅自动假定它是文本文件而不是可执行文件,并将其加载到我的默认文本编辑器中。

It could have something to do with the script not actually being set to print anything out. 可能与脚本没有真正设置为打印任何内容有关。 Remember, while all statements get printed output if run directly in the interpreter, for example, 请记住,虽然所有语句如果直接在解释器中运行都会获得打印输出,例如,

>>> a = 2
>>> a ** a
4

would show in the intrepeter, but you would need a print statement (function in P3k) for the output to be shown if run as a script. 会在说明器中显示,但是如果以脚本运行,则需要print语句(P3k中的函数)才能显示输出。

You have to have something to output the items to the terminal like for your file.py: 您必须要有一些东西才能将项目输出到终端,例如您的file.py:

#!/usr/bin/python

print "test"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM