简体   繁体   中英

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

I'm trying to switch from Notepad++ to Atom, but I just can't manage to get my scripts executed in 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.

In Notepad++ NPPexec I used to

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":

{
    "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?


I already tried to set my init-script to:

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 Script and type

C:\\Python34\\python.exe

it works. But thats not a permanent solution.


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

在此处输入图片说明

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

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


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.


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.

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

Inside grammars.coffee find:

  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

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

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":

在此处输入图片说明

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

在此处输入图片说明

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.

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.

Instead, there sa 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.

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

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

set PATH=%PATH%;C:\Python34

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


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

在此处输入图片说明

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

After all these steps:

  • Install Python with Add to Path
  • Install Atom
  • Install script package

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


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 everything is certainly not necessary, simply updating/repairing the installation with the installer is sufficient.

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.

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

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

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

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

"NB 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.

编辑你的 python.coffee 脚本

$ sudo nano .atom/packages/script/lib/grammars/python.coffee

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

for running with python3

#!/usr/bin/env python3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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