如何设置 Atom 的脚本来运行 Python 3.x 脚本?与 Windows 7 Pro x64 的组合可能是问题吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32006771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:51:51  来源:igfitidea点击:

How to setup Atom's script to run Python 3.x scripts? May the combination with Windows 7 Pro x64 be the issue?

pythonpython-3.xatom-editor

提问by thewaywewalk

I'm trying to switch from Notepad++ to Atom, but I just can't manage to get my scripts executed in Atom.

我正在尝试从 Notepad++ 切换到 Atom,但我无法在 Atom 中执行我的脚本。

I followed this answer(so I already installed script) which is not really extensive and also the rest on the web doesn't offer anything comprehensible for beginners.

我遵循了这个答案(所以我已经安装了script),它并不是很广泛,而且网络上的其余部分也没有为初学者提供任何可以理解的东西。

In Notepad++ NPPexec I used to

在 Notepad++ NPPexec 我曾经

NPP_SAVE
cd "$(FULL_CURRENT_PATH)"
C:\Python34\python.exe -u "$(FULL_CURRENT_PATH)"

and in Sublime Text 2 I made it run by creating a new "Build System":

在 Sublime Text 2 中,我通过创建一个新的“构建系统”来运行它:

{
    "cmd": ["C:\python34\python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

Can you please guide me how to setup Atom to be able to execute Python scripts with Python 3.4 scripts with a keyboard short-cut?

你能指导我如何设置 Atom 以便能够通过键盘快捷键使用 Python 3.4 脚本执行 Python 脚本吗?



I already tried to set my init-script to:

我已经尝试将我的 init-script 设置为:

process.env.path = ["C:\Python34\python.exe",process.env.PATH].join(";")

respectively

分别

process.env.path = ["C:\Python34",process.env.PATH].join(";")

with no success.

没有成功。



When I go to Packages -> Script -> Configure Scriptand type

当我去Packages -> Script -> Configure Script并输入

C:\Python34\python.exe

it works. But thats not a permanent solution.

有用。但这不是一个永久的解决方案。



When I press Ctrl+Shift+Bto run a script, without configuring it before (as it is supposed to work), I get (suggestion of ig0774's comment implemented):

当我按Ctrl+Shift+B运行脚本时,之前没有对其进行配置(因为它应该可以工作),我得到(实现了 ig0774 的评论的建议):

enter image description here

在此处输入图片说明

(it doesn't matter whether it is C:\Python34or C:\Python34\)

(不管是C:\Python34还是C:\Python34\

It complains that python is not in my path- but it is.

它抱怨python 不在我的路径中- 但它是。



I read multiple times that Windows 7/8 64bit together with Python 3.x could cause issues with certain packages.May this be the reason in ths case as well? I have Windows 7 Pro x64.

我多次读到 Windows 7/8 64 位与 Python 3.x 一起可能会导致某些软件包出现问题。这可能也是这种情况下的原因吗?我有 Windows 7 Pro x64。



Update

更新

As I've switched to VSCode and probably stay there, I'm not willing/don't have the time to try out all the answers, so I let the community judge the answers and accept always the highest voted. Please ping me, if it's not correct anymore.

由于我已经切换到 VSCode 并可能留在那里,我不愿意/没有时间尝试所有答案,所以我让社区判断答案并始终接受投票最高的答案。请ping我,如果它不再正确。

采纳答案by Matt Nona

This can be easily solved by editing the /home/.atom/packages/script/lib/grammars.coffeefile (note that the atom folder is hidden so you might have to press ctrl+Hto view hidden files and folders)

这可以通过编辑/home/.atom/packages/script/lib/grammars.coffee文件轻松解决(请注意,atom 文件夹是隐藏的,因此您可能必须按ctrl+H才能查看隐藏的文件和文件夹)

Inside grammars.coffeefind:

里面grammars.coffee找到:

  Python:
    "Selection Based":
      command: "python"
      args: (context)  -> ['-u', '-c', context.getCode()]
    "File Based":
      command: "python"
      args: (context) -> ['-u', context.filepath]

and replace with:

并替换为:

  Python:
    "Selection Based":
      command: "python3"
      args: (context)  -> ['-u', '-c', context.getCode()]
    "File Based":
      command: "python3"
      args: (context) -> ['-u', context.filepath]

Save changes, restart Atom and enjoy running your scripts with python 3

保存更改,重新启动 Atom 并享受使用 python 3 运行脚本的乐趣

EDIT: On Windows I believe the grammars.coffeefile is located in C:/Users/Your_Username/AppData/Local/atom/packagesAgain, the AppDatafolder is hidden so you might have to change your settings to view hidden files and folders.

编辑:在 Windows 上,我相信该grammars.coffee文件位于 C:/Users/Your_Username/AppData/Local/atom/packages再次,该AppData文件夹是隐藏的,因此您可能必须更改设置才能查看隐藏的文件和文件夹。

回答by thewaywewalk

Setting the PATHwithin Atom did not work, setting it with the cmd, via

在 Atom 中设置PATH不起作用,使用 cmd 设置它,通过

set PATH=%PATH%;C:\Python34

neither, and setting it in the Windows 7 system properties failed as well.

两者都没有,并且在 Windows 7 系统属性中设置它也失败了。



However reinstalling Python 3.4and check Add python.exe to Path

但是重新安装 Python 3.4并检查Add python.exe to Path

enter image description here

在此处输入图片说明

seems to be neccesary. Also I needed to uninstall Atomcompletely (inculding all packages or a least script) and reinstall it from scratch.

似乎是必要的。此外,我需要完全卸载 Atom(包括所有软件包或最少脚本)并从头开始重新安装它。

After all these steps:

在所有这些步骤之后:

  • Install Python with Add to Path
  • Install Atom
  • Install script package
  • 使用添加到路径安装 Python
  • 安装原子
  • 安装脚本包

it works out of the box (Ctrl+Shift+B) and no further steps are required.

它开箱即用 (Ctrl+Shift+B),不需要进一步的步骤。



I still don't know what was the reason before and I don't know which of this steps are really required.So feel free to include your procedure without reinstalling everything.

我仍然不知道之前的原因是什么,我不知道真正需要哪些步骤。因此,请随意包含您的程序,而无需重新安装所有内容。



Update

更新

Reinstalling everythingis certainly not necessary, simply updating/repairing the installation with the installer is sufficient.

重新安装一切当然不是必需的,只需使用安装程序更新/修复安装就足够了。

回答by dcosmic

same problem just like you. 'Packages -> Script -> Configure Script' is not permanent. So I has tryed another script runner:https://atom.io/packages/atom-runner, just found the problem is in the python script itself.

和你一样的问题。“包 -> 脚本 -> 配置脚本”不是永久性的。所以我尝试了另一个脚本运行程序:https: //atom.io/packages/atom-runner,刚刚发现问题出在 python 脚本本身。

When I use atom-runner, I got error message like this: atom-runner error

当我使用 atom-runner 时,我收到如下错误消息: atom-runner error

So it remind me that in the beginning of the python script: ' #!/usr/bin/env python3'

所以它提醒我在python脚本的开头:' #!/usr/bin/env python3'

It's obvious that the ENV_PATHis WRONG here. I should revise it in my python script.

很明显,这里的ENV_PATH是错误的。我应该在我的 python 脚本中修改它。

回答by Adam.Chang

Use the script-runner https://atom.io/packages/script-runner/

使用脚本运行器https://atom.io/packages/script-runner/

"N.B. these keyboard shortcuts are currently being reviewed, input is welcome. Command Mac OS X Linux/Windows Run: Script ctrl-x alt-x Run: Terminate ctrl-c alt-c"And "Run Terminate" (Alt + c) to use the current python in your system.

“注意这些键​​盘快捷键目前正在审核中,欢迎输入。命令 Mac OS X Linux/Windows 运行:脚本 ctrl-x alt-x 运行:终止 ctrl-c alt-c”和“运行终止”(Alt + c)在您的系统中使用当前的python。

回答by CreativeX

For Linux and Mac, adding environment in the script will pick correct python version. (command+ Ito run)

对于 Linux 和 Mac,在脚本中添加环境将选择正确的 python 版本。( command+I运行)

for running with python3

用于运行 python3

#!/usr/bin/env python3

回答by Rookie

Following up on Matt Nona's advice , when Atom starts-> Welcome Guide (or control+shift+T)-> 5th one down 'Hack on the Init Script'. A blank page will open and you can add that modifications in there.

遵循 Matt Nona 的建议,当 Atom 启动时-> 欢迎指南(或 control+shift+T)-> 第五个“Hack on the Init Script”。将打开一个空白页面,您可以在其中添加修改。

回答by Daniel Chamorro

To expand on @matt-nona answer. You can go to his mentioned config file right from Atom. Simply go to settings then "Open Config Folder":

扩展@matt-nona 答案。您可以直接从 Atom 转到他提到的配置文件。只需转到设置然后“打开配置文件夹”:

enter image description here

在此处输入图片说明

Then /packages/script/lib/grammars.coffee Find "Python" and make the appropriate change to python3:

然后 /packages/script/lib/grammars.coffee 找到“Python”并对 python3 进行适当的更改:

enter image description here

在此处输入图片说明

回答by its_broke_again

Update: for any other souls looking for this answer - On my Mac I do not have a grammars.coffee file within atom script config file.

更新:对于寻找此答案的任何其他人 - 在我的 Mac 上,我没有在 atom 脚本配置文件中的grammars.coffee 文件。

Instead, there s a grammars folder, and I have a python.coffee file in there. The same changes outlines in the screenshot (ie add '3' to the end of the two mentions of python) fixed my issue and atom automatically runs Python3 now.

相反,有一个语法文件夹,我在那里有一个 python.coffee 文件。屏幕截图中的相同更改大纲(即在两次提及 python 的末尾添加“3”)修复了我的问题,现在 atom 自动运行 Python3。

Not sure if the above answers are Windows specific or if there have been dev changes since 2017.

不确定上述答案是否特定于 Windows,或者自 2017 年以来是否有开发更改。