Python Script Runner

  1. To run Python script on a text editor like VS Code (Visual Studio Code) then you will have to do the following: Go in the extension section or press ‘ Ctrl+Shift+X ’ on windows, then search and install the extension named ‘ Python ’ and ‘ Code Runner ’. Restart your vs code after that.
  2. In addition to Kusalananda's answer, if you want the entire script to be run by python you can just change the first line to #!/usr/bin/env python3 and run it like any normal shell script. That way you don't have to remember what script you have to run with which interpreter.
  3. Running the.py script from the Terminal. Running the Python script from the terminal is very simple, instead of writing the Python script in the terminal all you need to do is use a text editor like vim, emacs or notepad and save it with a.py extension.

On Windows, the Python 3.4 interpreter is located at C:Python34python.exe.Alternatively, the convenient py.exe program will read the shebang line at the top of the.py file’s source code and run the appropriate version of Python for that script. The py.exe program will make sure to run the Python program with the correct version of Python if multiple versions are installed on your computer.

This package will run various script files inside of Atom, and uses a proper terminal for output. It currently supports JavaScript, CoffeeScript, Ruby, Python, Bash, Go and anything with a shebang line.

Usage

N.B. these keyboard shortcuts are currently being reviewed, input is welcome.

CommandMac OS XLinux
Script Runner: Runctrl-xalt-x
Script Runner: Shell

Scripts which have been saved run in their directory, unsaved scripts run in the workspace root directory. Selecting a single line will only run that one line.

A right-click context menu can be used to send signals to the running process, and as you may expect, you can interact directly with the terminal, typing input, pressing ctrl-c, and so on.

Closing a terminal will cause its process to be killed.

Run Selection

When invoking the above Run: Script command, if a portion of the script is selected, only that portion will be executed.

User Input

When running a script, the focus will be passed to the script output terminal. You are welcome to use the keyboard and mouse to interact with the running program.

Shebang Lines

In a typical UNIX environment, the shebang line specifies the interpreter to use for the script:

The shebang line is the preferred way to specify how to run something as it naturally supports all the intricacies of your underlying setup, e.g. Ruby's rvm, Python's virtualenv.

Even for unsaved files without an associated grammar, as long as you have the correct shebang line it will be executed correctly.

Environment Variables

The default Atom process takes environment variables from the shell it was launched from. This might be an issue if launching Atom directly from the desktop environment when using, say, RVM which exports functionality for interactive terminal sessions.

To ensure consistent behavior, when running a script, environment variables are extracted from the interactive login shell. This usually loads the same environment variables you'd expect when using the terminal.

Configuration

Split Direction

It is possible to configure which way to split the new pane. Open your Atom config file and edit 'script-runner'.splitDirection, the possiblevalues are: 'bottom', 'left' and 'right'. For example:

Scrollback Distance

To limit the number of lines kept in the output window simply edit the 'script-runner'.scrollback option inyour Atom config file.

Contributing

Python Script Runner
  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Released under the MIT license. Please see LICENSE.md for the full license.

If you can't execute or run a Python script, then programming is pointless. When you run a Python script, the interpreter converts a Python program into something that that the computer can understand. Executing a Python program can be done in two ways: calling the Python interpreter with a shebang line, and using the interactive Python shell.

Run a Python Script as a File

Generally programmers write stand alone scripts, that are independent to live environments. Then they save it with a '.py' extension, which indicates to the operating system and programmer that the file is actually a Python program. After the interpreter is invoked, it reads and interprets the file. The way Python scripts are run on Windows versus Unix based operating systems is very different. We'll show you the difference, and how to run a Python script on Windows and Unix platforms.

Run a Python script under Windows with the Command Prompt

Runner

Windows users must pass the path of the program as an argument to the Python interpreter. Such as follows:

C:Python27python.exeC:UsersUsernameDesktopmy_python_script.py

Note that you must use the full path of the Python interpreter. If you want to simply type python.exe C:UsersUsernameDesktopmy_python_script.py you must add python.exe to your PATH environmental variable. To do this, checkout the adding Python to the PATH environment article..

Window's python.exe vs pythonw.exe

Note that Windows comes with two Python executables - python.exe and pythonw.exe. If you want a terminal to pop-up when you run your script, use python.exe However if you don't want any terminal pop-up, use pythonw.exe. pythonw.exe is typically used for GUI programs, where you only want to display your program, not the terminal.

Run a Python Script Under Mac, Linux, BSD, Unix, etc

On platforms like Mac, BSD or Linux (Unix) you can put a 'shebang' line as first line of the program which indicates the location of the Python interpreter on the hard drive. It's in the following format:

A common shebang line used for the Python interpreter is as follows:

You must then make the script executable, using the following command:

Unlike Windows, the Python interpreter is typically already in the $PATH environmental variable, so adding it is un-necessary.

You can then run a program by invoking the Python interpreter manually as follows:

Python Execution with the Shell (Live Interpreter)

Assuming that you already have Python installed and running well (if you're getting an error, see this post), open the terminal or console and type 'python' and hit the 'Enter' key. You will then be directed immediately to the Python live interpreter. Your screen will display a message something like:

2
4
Python3.3.0(default,Nov232012,10:26:01)
[GCC4.2.1Compatible Apple Clang4.1((tags/Apple/clang-421.11.66))]on darwin
Type'help','copyright','credits'or'license'formore information.

Python Script Example

The Python programmer should keep in mind one thing: that while working with the live interpreter, everything is read and interpreted in real-time. For example loops iterate immediately, unless they are part of function. So it requires some mental planning. Using the Python shell is typically used to execute code interactively. If you want to run a Python script from the interpreter, you must either import it or call the Python executable.