Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to make a package in Python
aguy11 (127)

Notice: This tutorial does not explain how to publish a package, because it cannot be done in repl.it. To see the guide for publishing a package go here: https://packaging.python.org/tutorials/packaging-projects/

How to make a package in Python

Hello everyone! This is a tutorial on how to make a Python package. I will be making a package called AllPredict which has one function that predicts your chance of drawing a value out of a set of values.

First Steps

First, go over to the repl.it home page, and click 'All Languages', which should be below your most-used computer languages. Then find and select Python:

Once you've done that, name your file whatever you are going to call your package, (which in my case would be AllPredict), and create a folder under the same name. You're all set!

Making the main files

Next, we will need to code up our package (because Python never goes 'Boom!' to make something appear). For this, simply go over to your already-created folder and add a file that will be responsible for being a function in your package. (You can add as many of these as you want)

Then, in this package, create a class containing an init function, and some other functions that this part of your module is supposed to do:

class PredictDigit(object):
  def __init__(self):
    self.true_digit = 0
    self.digit_percentage = ""
    self.all_digits = []

  def predict(self, digits, digit):
    try:
      digits = list(digits)
    except TypeError:
      raise TypeError("Inappropriate Values Type")
    try:
      digit = int(digit)
    except TypeError:
      raise TypeError("Predicted Digit must Be INT")
    self.all_digits = digits
    self.true_digit = digit
    digit_num = 0
    denominator = len(digits)
    for i in digits:
      if i == digit:
        digit_num += 1
    percent = (100 / denominator) * digit_num
    self.digit_percentage = f"{percent}%"
    return self.digit_percentage

So, at the beginning of the program, I create a class called PredictDigit and define the init function, which is also called the constructor because there is no need to call it as it is called whenever you use this class. In the function I define 3 properties of this class(true_digit, digit_percentage, all_digits). Next, I define the predicting function which uses two parameters to predict a chance.

Remember: You can create as many of these as you want.

init.py

In this part of this tutorial, we will be creating a simple file that is responsible for all of the times you use this module. Note that this might seem like it serves no purpose, but it is important for if you decide to publish your package.

Inside of this file, for every module in your package type use the following structure:

from (package_name) import (module_name)

So mine would look like this:

from AllPredict import predictDigit

README.md and LICENSE

Now, you don't have to create these files if you're not going to publish your package, but if you are going to jump into the aforementioned, you will need to create two files:

  • README which is your package's description, and
  • LICENSE which is your copyright page

Let's create the REAMDE first:
For this, as you probably already understand, just create a new file called README.md which will use Github flavored Markdown to explain everything about your package. Make sure that your explanation is understandable. When you're done with that, get ready to create your LICENSE file!

LICENSE:
First, go over to https://choosealicense.com/ and select a license that best fits your package. Copy that license (Command/Control + C). Now, go over to your file and create a new file called LICENSE. This file needs absolutely no extension. Inside that file, paste your license and modify it so that it is visible that it's yours (Just modify it so that at the top, or wherever you see the copyright symbol there is the year and your project's name).

You are done with this tutorial now! Well, not really. As stated in the beginning, there is no way I can explain how to publish a package, because it is impossible in repl.it, but you can always scroll up and find that link that will explain it, but not on a web-based application.

If you have any questions, just comment down below!
Hopefully, I explained everything properly. You can now mess around with your package in the main.py. If you're ready to publish it, go ahead!

Comments
hotnewtop
Flyingcrabs (7)

what happend if i seitch to a new repl will i still be able to import the package?

aguy11 (127)

@Flyingcrabs If you follow the tutorial linked up top everything should work.

puremintwater (6)

i keep getting errors and its so annoying