Loading video player…

Setting Up Your Python CI Project

In this video, you’ll learn how to set up the Python project for Continuous Integration (CI). Therefore, a GitHub repository is created, the project is cloned and some basic library functionality is implemented.

Here’s the calculator.py file used in the example code:

Python
"""
calculator.py
Calculator library containing basic math operations.
"""

def add(first_term, second_term):
    return first_term + second_term

def subtract(first_term, second_term):
    return first_term - second_term

You can download the full CalculatorLibrary sample project at the link below:

Download

Sample Code (.zip)

2.8 KB

00:00 Now that you know the basics behind CI, it’s time to set up your own pipeline. Because the goal was to learn how to CI, the example project and Git process will be rather simple.

00:10 To get started, log into your GitHub account. If you don’t have one, go ahead and create an account. The free tier will be perfect for this project. And create a new repository.

00:20 Go ahead and call this one CalculatorLibrary.

00:26 You can leave the Description blank. Select either Public or Private. I’m going to leave mine Public. And check the box to add a README, and then also add a .gitignore.

00:36 Then you can just start typing in “Python” and you’ll get a Python-based .gitignore file. And Create! Okay, now that that’s there, you can go to your editor or your terminal and you can clone this.

00:50 I’m just going to do git clone

01:04 like that. And you should end up in your directory with the folder containing your Git repo. If you’re on PC, these commands won’t work and I recommend using the Git Bash from GitForWindows.org, but any Git client should work. Now that you have the repo cloned to your machine, you need to make and activate a virtual environment.

01:25 So going back to the terminal, just going to run python and then because we’re going to be opening the venv (virtual environment) module, we’ll do that.

01:32 And you can just call this calculator. Once that’s been created, you can go and activate it, so . calculator/bin/activate. And you’ll know this is working because you’ll see the name of the virtual environment in parentheses in your prompt. Keep in mind, if you’re on Windows, you’ll have a slightly different way of activating it and you’ll need to call the name of the folder, and then you’ll have to go into the Scripts folder and then activate. Okay, once you have your virtual environment activated, in the top level of your repo, go ahead and make a new file and just call this calculator.py.

02:16 And inside this, I’m just going to copy and paste a set of code.

02:23 And this is just going to contain the first two of the four functions that you’ll need to write for your calculator library. Go ahead, save this, and if you look, your CalculatorLibrary/ folder should have a .git/ folder, a .gitignore file, this calculator.py, and the README. If that’s all correct, back to your terminal or whatever Git client you’re using, cd into the CalculatorLibrary/ folder, go ahead and add the calculator.py, and commit these changes. And just put a message in there, something like 'Add functions for addition and subtraction'.

03:07 All right! You’ve got the base of the project set up. In the next video, you’re going to add some unit tests so that the pipeline can check functionality of the code. Thanks for watching.

Avatar image for carykinsfather

carykinsfather on Aug. 2, 2019

If on MacOS you may need to put python3 -m venv project_name_here.

Avatar image for Oleksii Potapenko

Oleksii Potapenko on Oct. 28, 2019

On Windows could not activate as described. It works as depicted here: medium.com/@presh_onyee/activating-virtualenv-on-windows-using-git-bash-python-3-7-1-6b4b21640368

Avatar image for Victor Amadi

Victor Amadi on Dec. 17, 2019

I agree with @carykinsfather.

Avatar image for Dan Bader

Dan Bader RP Team on March 5, 2020

@vicky That’s a private repository on Joe’s GitHub account. As part of this lesson you’ll need to create a new repository under your own GitHub account as explained at the start of the video. Your URL for the git clone command will be different, eg. github.com/YOUR_USERNAME/CalculatorLibrary/

To learn more about working with GitHub, also check out our dedicated course at realpython.com/courses/python-git-github-intro/

Avatar image for j-i-m-s-t-e-r

j-i-m-s-t-e-r on July 10, 2020

Where are we supposed to paste the content for calculator.py from? I mean, I can type it in manually based on what I see in the video, but for the sake of what the tutorial is teaching, we could do with having the code to paste in to save time (and typos).

Avatar image for Dan Bader

Dan Bader RP Team on July 13, 2020

@j-i-m-s-t-e-r: Thanks, we’ve just added the calculator.py file as a course download under Supporting Material and also made it available as an embedded code example on this lesson. Hope that helps you out :)

Avatar image for j-i-m-s-t-e-r

j-i-m-s-t-e-r on July 14, 2020

Hi Dan - I did see that was there yesterday as I carried on with the course, slapped my forehead, and was about to type here that I was stupid and somehow missed the glaringly obvious Supporting Material that is now there!

Thanks bud!

Avatar image for j-i-m-s-t-e-r

j-i-m-s-t-e-r on Nov. 23, 2020

I cannot get this to work on my new Mint20 box.

I am using vs code as an editor.

I have python3 installed.

I created the new repo in github.

I cloned it.

I cd’d into the CalculatorLibrary folder.

I got the python3 -m venv calculator run.

I did pip install flake8 pytest pytest-cov.

I pasted the contents of calculator.py from the sample code zip into the editor and saved it.

Running flake8 --statistics goes through the whole subfolder structure and throws loads of errors about lines being too long in all the files in the subfolders being too long eg. ./calculator/lib/python3.8/site-packages/pytest_cov/engine.py:275:80: E501 line too long (85 > 79 characters)

I tried therefore just flake8 calculator.py --statistics and it gives: calculator.py:15:36: W292 no newline at end of file 1 W292 no newline at end of file

So I put a new line in at the end of the final line.

Now it gives: calculator.py:16:1: W391 blank line at end of file calculator.py:16:1: W293 blank line contains whitespace 1 W293 blank line contains whitespace 1 W391 blank line at end of file

I have lost the will to live.

Avatar image for j-i-m-s-t-e-r

j-i-m-s-t-e-r on Nov. 23, 2020

Newline error was being caused by VS Code automatically inserting a TAB into the final new line.

I noticed the venv “calculator” folder in the example was above, not in, the CalculatorLibrary folder.

So I deleted the calculator folder inside CalculatorLibrary (it wasn’t too clear to me where this had to be done in the folder structure) and re-did the venv above it, in my home folder.

Still flake8 goes through all the library files too.

What gives?

Avatar image for Ricky White

Ricky White RP Team on Nov. 24, 2020

Hi @jimster. I believe we helped you solve your flake8 questions in the Slack community. If this is still an issue, reach out to me on Slack.

Avatar image for JBrooks

JBrooks on June 3, 2022

Did we specify a Python version for the virtual environment? How could I find this out or just use my global version?

Avatar image for Martin Breuss

Martin Breuss RP Team on June 8, 2022

JBrooks the virtual environment uses your base Python version, so whichever one you see when you check with:

$ python --version

That is, assuming that you used the same command for creating the venv as shown in the lesson:

$ python -m venv calculator

The crucial point here is that you may have different Python versions installed on your system, and you could create a venv with either of them. E.g. python3 -m venv calculator might use a different Python version than if you use python – because these commands might point to different Python executables with different versions.

So it’s a good idea to check what Python version your python command points to before using it to create a venv.

Avatar image for JBrooks

JBrooks on June 8, 2022

Got it! Thanks for the follow-up! I learned a lot.

Become a Member to join the conversation.