12
votes

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

enter image description here

(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.

9
Don't really know Atom, but your PATH should point to the folder containing the executable rather than the executable itself, e.g. process.env.path = ["C:\Python34", process.env.PATH].join(";") - ig0774
@ig0774 - probably you're right, but that doesn't seem to be the solution. (see edited screenshot) - thewaywewalk

9 Answers

21
votes

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.

14
votes

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

enter image description here

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

enter image description here

1
votes

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.

1
votes

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

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

0
votes

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.

0
votes

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

enter image description here

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.

0
votes

Use the 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.

0
votes

edit your python.coffee script

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

-3
votes

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