Intro to Python Programming – Part 0: Set Up

Hello! Welcome to the first part of the Introduction to Python Programming series. In this post, we’re going to go over how to set up Python, a code editor and also how/why to use virtual environments for your projects. We won’t really be writing any code in this post, so if you’re ready to start the exciting stuff, go check out part 1.

 

Step 1: Installing Python

Installing Python is going to be a bit different depending on what operating system you’re using. I’ll go through Windows, Mac and Linux here. If you have any issues of questions following this post, please leave a comment and I’ll help you fix any issues.

 

Windows

If you’re on windows the first thing you need to do is head over to https://www.python.org/downloads/windows/. This will take you to the download page for the latest release of Python for windows. There are a few different installation options there, but the Windows x86 Executable Installer should be fine.

Once it’s downloaded, just follow the installation instructions. To verify its downloaded correctly, open the command prompt (click on the windows icon at the bottom left of your screen and type cmd). Once this is open, type python and you should see something like this:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

If you have any problems please leave a comment to this post and I’ll get back to you. Sometimes there can be problems if a thing called the pythonpath isn’t set correctly. The pythonpath is what’s called an environment variable it tells the computer where python is found. An environment variable is essentially just a name, and a corresponding value (called a key, value pair in programming… we’ll get to them later). These are used by the operating system to store information about it’s environment (for example, what version of Windows is running? where are certain important folders? etc). Environment variables are common to all operating systems, they’re just implemented slightly differently.

 

Mac

Depending on your version of macOS, you might actually already have Python installed. Open your terminal (if you can’t find it press cmd + spacebar to open your search bar, type terminal and press enter, a terminal window will open up). Once your terminal is open type python, if it is already installed, you’ll see something similar to the output above.

If it is not already installed, head over to https://www.python.org/downloads/mac-osx/ and download the latest version of Python there. Once it’s downloaded, run the installer and follow through the instructions (pretty much just keep clicking continue until its finished). Once its finished, you should be good to go.

 

Linux

If you’re on Linux, I’m going to assume you are comfortable with the terminal. Python should be included with Linux and can be run from the terminal by just running python. If for some reason you don’t have it already installed, simply run:

$ sudo apt-get update
$ sudo apt-get install python3.6

 

Step 2: Code Editor

Ok, so now we have Python installed and working, we need something to help us write some code. Python comes with an editor called IDLE, which is alright for running quick snippets of code, but I really don’t like it. I’d actually choose to use the command line instead of IDLE. This is OK for testing quick bits of Python, but if we want to build anything larger, I’d strongly recommend downloading a code editor. I find using an editor really useful as (among other things) they offer code completion (like autocomplete on your phone but for code), text highlighting and usually also a directory browser which really helps when you’re working with lots of files and folders. Currently, I am using Visual Studio Code, but there are plenty of good ones out there (PyCharm is a really great choice if you’re up for spending a bit of cash). I wont go into installing a code editor here as its pretty much just the same as installing any other app, but if you run into any problems let me know and I’ll try to help.

 

Step 3: Create a Virtual Environment

In this last part of the post, I really want to mention Virtual Environments. Basically, a virtual environment is a separate copy of Python which you use for a specific project. This has the advantage of allowing you to work on a project without worrying about any conflicts with other projects. This becomes more important as you start to install and import more external packages. A package is pretty much just a collection of python files you can import into your project to add extra functionality. So by using a virtual environment, when it comes to releasing a project to people, you’re only going to be including packages needed for that project, instead of including the packages for all your projects.

So lets create a virtual environment for these tutorials. Open up terminal, or command prompt if you’re on Windows and run the following:

python -m virtualenv tutorial_env

So what does that command actually do? We’re basically telling python to import and run the virtualenv package as a script (using the -m flag) to create a new virtual environment in our current directory called tutorial_env.

It might take a few seconds to complete, but when it does, you should see something like this:

Using base prefix ‘C:\\Python36’
New python executable in C:\directory_to_folder\tutorial_env\Scripts\python.exe
Installing setuptools, pip, wheel…done.

 

Once that’s done, when we want to work in our environment we just activate it:

On Windows command prompt:

tutorial_env\scripts\activate

On Mac or Linux terminal:

source tutorial_env/bin/activate

You should see (tutorial_env) prefix in the terminal now the environment is activated. Now, any packages we install will be associated with tutorial_env.

To deactivate the environment, just run the deactivate command.

 

Conclusion

OK I think that’s enough for now. Sorry this post was so wordy. I know this is a bit dull but I felt it was important to explain how to actually get everything set up just to make sure that anyone with absolutely no experience can follow these posts.

Thank you so much for reading, if you found this useful or have any questions, problems, or notice anything incorrect please do leave a comment below.

See you next time!

Leave a Reply

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