Installing and getting started with Python

I like experimenting with and learning new things. I’d never looked at Python before, because its syntax put me off, coming from a background of languages with C-like syntax. However, I eventually convinced myself to at least have a play with it and I’ve started working on a simple application that I can deploy to AWS.

Of course, the first step with any new tool is always getting it set up, and it can sometimes not be as straightforward as one would expect. I had a bit of trouble at first, so I thought I would share my experience for others that want a quick way of getting started.

Pyenv

The first thing I want to say is that I would strongly suggest using Pyenv. I always suggest version managers for programming languages, because not only do they allow you to have more than one version installed and be able to switch between them, but they also change the default paths for module installation to your user directory, meaning you don’t need sudo to install dependencies – a big advantage for me.

Dependencies

The first step to install pyenv is to install the dependnencies for building python. These vary on your operating system, but there is a guide over at the Pyenv github.

In my case, I use Ubuntu, so I had to run the following command:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev

Installation

Once that has been taken care of, it’s time to install Pyenv itself. Once again, follow instructions over at the github page for pyenv for your OS.

I used the automatic installer, which is the easiest way, but it also requires to install git in addition to the above dependencies.

After installing it, you need to add the following to your ~/.bash_profile or ~/.bashrc or equivalent and re-start your terminal session:

# Load pyenv automatically by adding
# the following to ~/.bash_profile:

export PATH="/home/errietta/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

If it’s worked, running pyenv should show you a help screen!

Install Python with pyenv

Now that you have pyenv working, you can easily install one or more versions of Python. Since I wanted to use it with serverless, I needed to get either 2.7.* or 3.6.*.

Run pyenv install 2.7.8 or pyenv install 3.6.6

If everything goes correctly, it should only take 5-10 minutes. If not, the output should say what the problem was – the common build problems page on github has some more information to fix problems, but generally it should just work if you have installed all the dependencies.

It should say “Installed Python-3.6.6 to /home/errietta/.pyenv/versions/3.6.6” when it is finished.

Hello world

Now you can make your first python code. First of all, inside the directory of your project, you should run pyenv local 3.6.6 (or whichever version you installed), so that pyenv knows which version of python to use for your project.

Now you can make your code file, say hello.py:

print("Hello world")

And to run it:

python hello.py

Congrats, it works!

Modules

Consider the following tree:

.
|-- hello.py
`-- util
    `-- math.py

And the following code in each file:

hello.py:

from util.math import add

print("Hello world")
print(add(2, 3))

util/math.py

def add(a,b):
  return a+b

This might work, but if you lint your code with pylint (or if your IDE does it for you – Hi VS code!), you’ll notice that it complains:

hello.py:1:0: E0611: No name 'math' in module 'util' (no-name-in-module)

What you need to do in this case, is create __init__.py with no content inside util. This tells python that your directory contains python modules. It can also execute initialization code, but in this case you can just leave it empty.

Now that should make pylint and/or your IDE happy 🙂

Next steps

One Comment

  1. Haii Errietta, i read your profile and I was very impressed with your skill. Can you give me your experience about java. My basic skill is a java desktop developer, but i am feel my skill can not up everyday. Can you give advice for my problem ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.