Python File Runner

The key function for working with files in Python is the open function. The open function takes two parameters; filename, and mode. There are four different methods (modes) for opening a file. Create Package Function File. Finally, we will need a python package function file which will contain the python code that will need to be converted to a function. In this demo, we are simply creating a function for a create table statement that can be run in Synapse or Databricks. It will accept the database, table. It's simple to run hello.py with Python. Just click the Run Python File in Terminal play button in the top-right side of the editor. The button opens a terminal panel in which your Python interpreter is automatically activated, then runs python3 hello.py (macOS/Linux) or python hello.py (Windows).

  1. Python File Runner
  2. Run Python Script Linux
  3. Py File Runner
  4. Python File Runner 2

Run your python (.py) source file using F5 or F6!

  • Atom Text Editor (nightly or latest stable release)
  • Python 2 and/or 3
  • Add Python (and any other interpreters) to the PATH environment variable.
  • Using python
    • Almost the same console with python IDLE, which provides syntax error and runtime error messages.
  • CodeBlocks debug console style
    • Shows return value and execution time
      • It is a rough time based on real time rather than CPU kernel / user time
  • Cross Platform Compatible

    • Runs on Windows, Mac OS X, and Linux
  • True Arbitrary Execution

    • Global python is the default interpreter
    • Execute using any interpreter
    • Pass options to the given interpreter
    • Pass arguments to the program to be executed
  • Python 2 and 3

    • Note: If you have problems executing, you can install a global version of latest python2.7.x (even if you have python3.x.x installed). Please report any python3 issues if you want to avoid installing a global python2 version.

This project has been documented in a fair amount of detail over time. This documentation can be found in the Wiki.

Everyone should take the time to reveiw the Wiki README at the bare minimum. It details an overview on how to handle issues, use different versions, and includes links to primary sections of the Wiki.

Everyone should also take the time to review the Wiki section How do I use atom-python-run?. It covers everything from installation, to configuration, logging, and much more. You just might be surprised by what you can do with atom-python-run.

You should have the basics after having covered both the README and How Do I use atom-python-run? sections. Most FAQ's can be resolved by simply reading them. The guides provided should allow us to help you with what ever issue you're facing.

NOTE: Be sure to read the Wiki and the Wiki README before reporting an issue or making a pull request. A lot of time has been put in to it to help you the user (or dev) get started and on your way.

  • Before newing an issue, check to see if someone else is experiencing any related issues.
  • Check to see if any issues that were closed resemble your issue and re-open it addressing that you're experiencing a similar issue.
  • Provide details about your issue, such as errors and/or logs.
  • Provide reproduction steps (we can't help you if we don't know how to reproduce the error!).

If you're a developer and are interested in this project you can find this repos API's in the Wiki. More specifically, you'll want to take a look at How does the cp module work? and How does the terminal.js module work? sections of the Wiki.

You can also just read the key source files

  • cp (cp is written in python)
  • New an issue if you have any idea of new features.

This is a package for Atom

In this part of the 'Learning Python' series, you'll learn how to create Python files and run them from the terminal. You'll be writing your very first Python script!

We'll be using Visual Studio Code to write our Python script and Cmder (the terminal emulator) to run it, but feel free to use your own tools.

Python files

Just like text files have the file extension .txt and HTML files have the extension .html, a Python file is characterised by the .py extension.

To create a Python file, simply save the filename as your_filename.py extension, being sure not leave any spaces in the filename.

You can call the filename whatever you like (with some exceptions which you'll learn about later in this series), however it's a good idea to give them a recognisable name, as opposed to something totally abstract like x.py.

We're going to create a new directory on the desktop using Cmder which we'll store all of our code in throughout this series.

We'll be creating sub directories within this folder for each module of the series too, so you'll be able to go back and look through the code for future reference.

Open up Cmder and navigate to your desktop (or wherever you'd like to create the directory)

We'll call the main directory learning_python

Teminal

Now move into the directory with the cd command:

Teminal

We'll create our first subdirectory called python_files using the mkdir command and move into it

Teminal

And finally, let's create our first Python file! We're going to use the touch command and name out file main,py:

Teminal

Let's write some Python code!

Writing Python code

Python code is written line by line in a .py file and is read from top to bottom.

We're not going to explain the code in detail (it's only a couple of lines) as you'll be learning about it in the next part of this series.

You can open up Visual Studio Code from Cmder with the following command:

Teminal

This command will open up VS Code in the current directory you're in. You could also do the same by right clicking on the learning_python directory and selection 'Open with Code'.

The first line of Python (Feel free to change the name to your own!):

main.py

I don't want you to be too concerned about the code here, but so you know what we've done:

  • We've created a variable called my_name and assigned it the value of 'Julian' using the = operator
  • A variable is a placeholder to store a value or values, which we can then reuse over and over again
  • 'Julian' is a string, which in programming terms is a string of characters wrapped in quotations or apostrophes

Let's add one more line of code below:

main.py

Let's quickly look at the second line:

File
  • We're using the print function and passing in the my_name variable in between the parenthesis ()
  • The print function in Python is used to print something out to the terminal. Whatever values we pass in will be printed for us to see

print is one of the built-in functions that comes with Python and you'll be learning all about functions soon!

Can you guess what might happen when we run this file? Let's find out.

Tip - Make sure you save your Python file. Use Ctrl + s in VS Code to save it

Running Python files

Running a Python file (or scripts as they're commonly referred to) is extremely simple.

To run a Python file form the terminal, simply use the python command followed by the name of your file!

Python File Runner

For example (the opposing arrows <> show where you should replace the value for your own):

Terminal

If you're not in the same directory, you can also provide the full path to your Python file, like so:

Terminal

We called our file main.py, so we'll run it with the following:

Terminal

You should see the following output in your terminal:

Terminal

If you changed the value for my_name, you'll of course see that instead!

Let's spice up our script by printing out some more information.

Replace print(my_name) with the following:

main.py

Again, don't worry too much about what's happening here! You'll learn all about strings and variables in the next part of the series.

We're using something called 'string interpolation' to insert the my_name variable into another string, which we're then multiplying by 10 and passing it to the print function.

For now, go ahead and save the file and run it again form the terminal:

Run Python Script Linux

Terminal

You should see:

Terminal

Now wasn't that fun! ๐Ÿ˜„

Wrapping up

This guide was to teach you how to write and run Python files and I hope you've learned how easy it is.

Py File Runner

Key points to remember:

  • Python files must have the .py file extension
  • Don't use spaces in your filenames, instead, use underscores or dashes
  • Run a Python file from the terminal with the following:

Terminal

Next in the series - Variables and strings

Last modifiedยท27 Feb 2019
Previous articlePython Tools | Learning Python Pt. 1
Next articlePython variables | Learning Python Pt. 3
Did you find this article useful?
Related items

Harnessing the power of Pythons enumerations and exploring the enum module

A handy Python snippet for web developers

Building a Run-length encoder with Python for lossless string compression

Python File Runner 2

Two function variations to solve the popular FizzBuzz interview question

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

Comments are closed.