Before you go, check out these stories!

0
Hackernoon logoSetting up a Python Development Environment in Atom by@ethan.jarrell

Setting up a Python Development Environment in Atom

Author profile picture

@ethan.jarrellEthan Jarrell

Of course there are a lot of great text editors out there. Sublime Text, Brackets, Atom. I’ve always personally been a fan of Atom because it is completely free, and it has a lot of available packages and themes that makes coding a little easier. Here, I’ll look at how you can set up a “python friendly” development environment with Atom, some of the packages that are useful to coding in python, and then take a look at writing some basic code.

1) Downloading Atom

First thing’s first, if we’re going to use Atom as our text editor, we better download it. Here’s a link to do just that: https://atom.io/

2) Install a Syntax Theme

Once you have Atom installed, you can go to preferences, and in the preferences menu, select +install. Then, select the option for themes to download a theme. A syntax theme will have a color scheme that will make the code easier to read at a glance. I’ve found that certain themes seem to be great in JavaScript, but they aren’t as readable in Python. For example, I’ve always been a big fan of Atom Dark and One Dark, for my UI and Syntax themes. Up until recently I’ve mainly coded in JavaScript, React and Node, and this theme works great for me with those languages. However, in Python, I don’t like it as much. Finding a syntax theme that works for you can be difficult, especially once you get used to one theme.

Here are some of my favorites for Python:

1) Atom Material

This is the theme I’ll be using throughout this post, and is the one seen in the example above. I like it because a lot of themes tend to be somewhat monochromatic, and don’t seem to be as readable for that reason. Atom Material uses a lot of colors, and there is a high contrast, making it easy to find and read code. https://atom.io/themes/atom-material-syntax

2) Jackhammer

This one doesn’t have quite as wide of a use of color as Atom Material, but still a lot of contrast. https://atom.io/themes/jackhammer-syntax

3) PreDawn

Another great one. I’ve seen quite a few developers use this one, and seems very Python friendly. https://atom.io/themes/predawn-syntax

If you want to use one of these, or find your own, you can search for them in the search bar to the right of the themes button. Or do a generic search for “syntax” or “python”.

3) AutoComplete Python Package

Not everybody likes the auto-complete feature. And I agree, sometimes it can get in the way. But, if you’re like me, and just starting out in Python, it can be really useful. The autocomplete-python package gives you the option of being powered by either Jedi or Kite. Jedi is a locally based library, while Kite accesses an online library. Here’s more documentation on the package: https://atom.io/packages/autocomplete-python

4) File Icons Package

The file Icons package allows you to see the icon/logo for each file type you’re working on. If you only work in Python, or only JavaScript or whatever, then this is probably a non-issue. But more than likely your file tree can contain multiple files in multiple languages and formats. In which case, having this package will help you easily find files within your tree. https://atom.io/packages/atom-file-icons

5) Linter-flake8 package

This is a great modular source code checker. Once you install the package, you’ll also need to user the command line to complete the installation. Instructions on how to do it are all well documented. https://atom.io/packages/linter-flake8

6) Minimap package

Minimap isn’t python-specific, but is a great tool for any coding language. Once your code gets to be hundreds of lines long, it can be difficult to find where you are in the code base. Minimap provides a “zoomed out” view of the entire code, and highlights where you are in the code, keeping the entire visualization in a concise sidebar in the atom editor. https://atom.io/packages/minimap

7) python-autopep 8 package

autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pycodestyle. Once you have this package installed, you might want to click on settings and select the “Format on Save” option. This will also require to you complete the installation using pip on the command line, as you can see in the documentation. https://atom.io/packages/python-autopep8

8) script package

By far one of the most important packages on this list is the script package. This allows you to run scripts in the Atom editor using the “command + i” keyboard shortcut. The code will run in a panel at the bottom of the text editor. https://atom.io/packages/script

9) Getting started writing code in Python

One of my favorite ways to get started is finding a problem to solve. The Project Euler site has a number of math based problems that you can solve in any programming language. Since I am new to Python, I decided to take one of the project euler problems I had solved in JavaScript, and solve the same one in Python. Number 5 on project Euler reads like this:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

So in JavaScript, I solved the problem this way:

let number = 1;
while ( number % 1 !== 0 || number % 2 !== 0 || number % 3 !== 0 || number % 4 !== 0 || number % 5 !== 0 || number % 6 !== 0 || number % 7 !== 0 || number % 8 !== 0 || number % 9 !== 0 || number % 10 !== 0 || number % 11 !== 0 || number % 12 !== 0 || number % 13 !== 0 || number % 14 !== 0 || number % 15 !== 0 || number % 16 !== 0 || number % 17 !== 0 || number % 18 !== 0 || number % 19 !== 0 || number % 20 !== 0) {
number = number + 1;
}
console.log(number);

Basically here I have a while loop, starting at 1, it checks if the number is divisible by 1 through 20 with no remainder. If not, it adds one and checks again until it finds a match. So with Python, I tried to simply use the same code, but translate it to Python, which looked like this, and let my preface this with the fact that this is really not a good idea.

number = 1
while (number % 1 <> 0 or number % 2 <> 0 or number % 3 <> 0 or number % 4 <> 0 or number % 5 <> 0 or number % 6 <> 0 or number % 7 <> 0 or number % 8 <> 0 or number % 9 <> 0 or number % 10 <> 0 or number % 11 <> 0 or number % 12 <> 0 or number % 13 <> 0 or number % 14 <> 0 or number % 15 <> 0 or number % 16 <> 0 or number % 17 <> 0 or number % 18 <> 0 or number % 19 <> 0 or number % 20 <> 0):
number += 1
print number

Basically this does the same thing as the above code, but written for Python. A major difference that I quickly realized is that Python will print every single number from 1 until it reaches the answer, which can take a long time. So as I started trying to understand Python, here are some of the syntactical differences, that will help in getting started:

1) For Loops

I found that a normal for loop in JavaScript looks like this:

for (var i = 0; i < array.length; i++) {
array[i]
}

However, in Python, it looks more like the JavaScript “for in” loop:

for x in range(0, 3):
print "We're on time %d" % (x)

for loop from 0 to 2, therefore running 3 times.

2) Variables

In JavaScript, variables need to be defined by first calling “let, var or const”.

let x = 1
const y = 2
var z = 3
let my_array = [1, 2, 3, 4]

In Python, you can simply type the variable name without defining it as a variable.

x = 1
y = 2
z = 3
my_array = [1, 2, 3, 4]

3) Functions

In JavaScript, functions are called using “function” and can take a parameter, or multiple parameters:

function test_prime(n){
// do stuff
}

In Python, they are basically the same but are called using the keyword “def” instead.

def test_prime(n):
//do stuff

Another major difference between function calls is that in JavaScript, the work inside the function is always between curly braces, following the parameters. In Python, a function begins with a colon, and instead of curly braces, the function is anything indented below the line calling the function. With some of the packages we installed earlier, you’ll often see “unexpected indent” letting you know that you’ve indented something that doesn’t need to be there. This is a little hard to get used to, if you’re more familiar with something like JavaScript, where indentions don’t affect the code.

4) console log

In JavaScript, if you want to run a script, or block of code, you can simply console.log it

console.log(my_function);

In Python, you generally use the “print” command

print solution

With these building blocks in place, I then went on Stack Overflow and found a better solution for my original project Euler problem:

check_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
def find_solution(step):
for num in xrange(step, 999999999, step):
if all(num % n == 0 for n in check_list):
return num
return None
if __name__ == '__main__':
solution = find_solution(2520)
if solution is None:
print "No answer found"
else:
print "found an answer:", solution

Here’s a link to the problem on Stack Overflow: https://stackoverflow.com/questions/8024911/project-euler-5-in-python-how-can-i-optimize-my-solution

Here, we have a variable “check_list” which holds an array.

Inside the function “def find_solution” we step through the numbers 1 through 999999999, and check each number for divisibility against the numbers in our array. If it matches, we return the number, if not, we return none.

Then, we feed the solution the step of 2520, instead of checking each number, or every other number, it only checks every 2520th number, which allows the code to run much faster. Then it prints the solution, or if none were found, it prints that.

Obviously I’m fairly new in Python, but if you’re like me, and just getting started, hopefully this gives you a few things to consider. Thanks so much for reading!

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!