-
Notifications
You must be signed in to change notification settings - Fork 32
12 How Do I Use atom python run
- Introduction
- Pre-Install
- Post-Install
- Settings
- Hello World
- Cant save a buffer with no file
- Console Log
- cp Log
- Extension Filter
- Commands
- Piping
- Alternative Shells
- Summary
The package it self is actually fairly simple. If you are unfamiliar with how the Command Line Interface (CLI) works, it may be a bit tricky to set, navigate, and manipulate.
That's why I made this Wiki, README, and Guide to walk you through the basics.
This guide is less of a how-to and more of a visual reference than anything else. This way you have a visual aide and know what to look for (in a "physical" sense").
- install python (2 and 3 are both supported)
- make sure to add it to the
PATH
environment variable during installation. Theres usually a small checkbox that let's you mark this as an option. If, for some reason, you did not check off the PATH option when installing, there are some guides online that can walk you through this.- SuperUser [Windows]
- StackOverflow [Windows]
- PythonCentral.io [Windows]
- use Atom to install atom-python-run.
ctrl + ,
will open up the Settings panel.
After you have installed atom-python-run package in Atom, you can manipulate the settings, or you can use the default settings, and begin coding right away.
You can change and manipulate how atom-python-run executes a script. This basically means that you can run any interpreter. We will use the default interpreter, python, in our examples. In the settings panel (ctrl + ,
), you'll see quite a few options available. We'll go through each of them and explain what they do.
NOTE: Even though you can use other interpreters, you still need to have python installed. This package is dependent upon the python interpreter to work properly. I suppose you could always transpile to your chosen interpretered language; if you figure out how to get it to work using just nodejs, let us know!
Like all beginnings in the coding community, we can start off with a hello, world
application.
ctrl + n
to create a new buffer and panel in atom. Depending on which version of python you installed depends on how you can write it up. I usually go for forwards compatibility over backwards compatibility because python2
will be officially deprecated starting in 2020.
from __future__ import print_function
print('Hello, World!')
ctrl + shift + s
to save as
hello-world.py. then f5
or f6
to execute. atom's console.log
will create the initial log first which you can see to right in the image below.
NOTE: Always use a hello, world
application for testing atom-python-run. It will simplify everyone's life in the long run.
You can attempt to execute a buffer, but it will fail, and fail miserably.
You should always save your source code as a file first and then execute after. Any changes made afterwards can be saved and executed simultaneously.
atom-python-run will always attempt to save the file first before executing it. If not, you'll see something like this. So if you see a red bubble with the No File Error message, you'll know why.
Let's take a peek at the Console Log
first.
First, we have to open Atom's Console.
- How to access Atoms Console Log
- Windows/Linux
- Ctrl + Shift + I
- Mac OS X
- Cmd + Alt + I
- Click on the
console
tab - Copy and paste the console output along with your post.
- Windows/Linux
A panel to the right will now be displayed. We can click on the settings button to look at atom-python-run's settings too. We'll leave those as the default for now. You can always change them later to suite your needs.
We can move the console to the bottom of the window or detach it all together. Sometimes I detach it so I can see more and get a bit of flexibility. There is no rule or guideline for this. It's just preference.
What's important about the Console Log
option is using it to debug atom-python-run's behavior. Sometimes you might set up some custom settings, all of a sudden it doesn't work as expected, and this is how you can figure out why... sometimes. It's not a catch all. Just a general overview of what's happening.
When a problem occurs, you'll want to refer to the logs. They are there to help you, and us (the devs), to isolate the problem that your experiencing.
You can see the output in the console pane to the right.
NOTE: You can toggle whether this setting is On or Off in your settings. Turning it Off can improve execution speed of the package during development.
The cp Log
is typically used to debug why your program may not be executing properly. There is a thin layer where nodejs
passes off control to the cp
process that is created. If Atom manages to create a cp
process, a log is created keeping track of how your program was executed.
- How to access
cp
's built-in log- Windows
- Open file explorer
- Click the location bar (where the file path usually is)
- Type in
%userprofile%\.atom\packages\atom-python-run
- Locate, Open, and Copy the contents of the cp.log file along with your post.
- Mac OS X/Linux
- Open a terminal window.
$ cat ~/.atom/packages/atom-python-run/cp.log
- Copy and paste the contents along with your post.
- NOTE: If the
cp.log
file is missing, empty, or inaccurate, please note that this was case in your post.
- Windows
The cp
log is often confused with the console.log
. It's important to realize that they are different and log different pieces of information for different reasons. You can find the technical details in the wiki here.
Console Log
is temporarily written to Atom's Console. The cp Log
is written to a file on your local hard drive (or where ever you installed atom). You can tweak where these files are stored by manipulating the path in source code.
NOTE: cp
will always generate a log if it can. There is no option to toggle this On or Off.
- Extension filter
- Accepts all file extensions by default (empty value), you can change it into
.py
- or an array
.py, .js, .something-else
- or an array
- Accepts all file extensions by default (empty value), you can change it into
The following will give you a cursory overview if you're unfamiliar with the CLI (Command Line Interface) because Commands operate with the same fundemental ideas.
Commands are basically how you execute your programs. So it's important to know how to manipulate the variety of ways you can execute actions.
In Settings->atom-python-run->Settings, there are 2 command fields. F5 Command is field 1 and F6 Command is field 2. Each Command Field has a it's own Pause option.
You can choose to Pause
the shell after your program has finished executing. Text is typically display at the end of the output indicating the results.
What makes the Command Field so special are the interesting things you can ask it to do. Let's follow an example to showcase what I mean by this. Suppose the following code is a hello.py file.
from __future__ import print_function
print('Goodbye cruel world!')
Now suppose these are our current settings while attempting to execute the hello.py file.
When you execute, you'll see that you get Hello, World
instead of Goodbye cruel world!
in standard output. You should also notice a smaller execution time (depending on your system).
What's happened here? What socercy is this!? The thing is that you're not actually executing your source code when you press the F5 key to execute. You're executing the command that is mapped to that key instead.
In this scenario, we mapped the command echo "Hello, World!"
to the F5 Command. The F5 Command is mapped to the F5 key as well. So when you hit the F5 key, you execute the command echo "Hello, World"
instead of the intended file hello.py.
NOTE: In order to call different python binary versions, they must be named accordingly, and they must be set in the PATH environment variable! If the PATH environment variable is not set, your system won't know where python is, and will fail to execute. This includes any other interpreter you may wish to execute.
Now that we understand how atom-python-run executes Commands, we can tweak it a bit in our favor. Let's keep things simple. Let's say I have python 2 and python 3 installed. By default, my system uses python3, but I have a python2 program that I want to execute. Well, that's easy. We can just modify it to call python2 specifically instead.
Let's say we have a python source file called hello2.py.
print "Hello, World!"
If we tried to execute this and our global default was python3, then we would get an error during execution. Instead, we can tell atom-python-run that there is another version of python by specifying it explicitly.
Now we can execute hello2.py just fine without any errors.
NOTE: If you are editing a javascript, ruby, or some other source file and wish to run it with atom-python-run, just change it to the appropriate interpreter instead. Assuming node is installed and set within the PATH variable, it should run as expected. Just realize that you will still need the python interpreter to execute.
This is very much intentional by design. In order to have true arbitrary execution, you have to be able to execute anything. It's also important to realize that you are making a System Call when ever you execute. So heed caution.
WARNING: You should NEVER run Atom as admin/root. Running any nodejs
component as root
without understanding what's happening is ALWAYS dangerous. Of course, you shouldn't be running your text editor as admin! That's what editors like vim
are for! If you were to run a command such as rm -rf /
... it would be catastrophic.
Some interpreters include an Interactive Mode so you can run your script and drop your self right in to the interpreter it self. This allows you to play around with small snippets and test functions (for larger projects, you should use some type of unittest
utility).
I usually untick the Pause
option here since the Window will be kept open and active by the prompts. For python, it would look like this.
The last thing I'd like to cover are feeding your program arguments. Since Command Fields operate almost the same as a CLI would, it makes intuitive sense that we should be able to pass arguments as well. The good news is that you can!
Let's say we have a python program called greet-args.py.
from __future__ import print_function
from sys import argv
if __name__ == '__main__':
for arg in argv:
print('arg:', arg)
All we have to do is modify one of our Command Fields to reflect the fact that our program takes in arguments.
We should then get the expected results.
- Pipe to File
- Redirect standard output to a file instead of the screen
- Name the path and file to write to
/some/file/path/to/my-programs-output.txt
- Create the file in the current working directory by omitting a path
my-programs-output.txt
- Name the path and file to write to
- Redirect standard output to a file instead of the screen
atom-ptython-run will default to your Operating Systems default Shell when the Terminal field is empty. You can use other shells if the system default doesn't suite you as a convenience.
You can use an alternative shell by specifying it's name
and execution option
as a list.
atom-python-run will be able to use any shell as long as it is available via the PATH
environment variable.
You can do much more with atom-python-run!