Learn to Code via Tutorials on Repl.it!

← Back to all posts
Creating a Package in Python
CodingElf66

Creating a Package in Python

Intro

Hello, Replit Community! Today I'm going to teach you how to create a package in Python!

About

Packages are like built-in variables and functions that make your code easier. They can perform all sort of things, such as calculating the sine of an angle, or printing something in the console with color. Whatever it is, it's useful, and you should learn it! So, let's jump straight in to it!

Getting Started

You will need to create a Repl for this first (click on the + button. Name the Repl whatever you want, that's just the simple part.

Then, you go create a folder (just name it server). Inside, create a file this time named LICENSE, and paste the following text inside. You will need to change the year and username if necessary, but just keep this for now.

Copyright (c) 2021 CodingElf66

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringment. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contact, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in the
software.

Then, start creating your file. Your file should just contain functions, like the ones shown below. But, if you want to, you can add classes, then functions.

The last part will be creating a documentation that will explain how your package works, and saying its' functions and what it do, etc.. So, create a file called DOCUMENTATION.md (the .md stands for Markdown, you can use it to style your documentation). I will use mines as an example.

Math Pacakge

Math is a simple Python Package (supported in Python 3.0). It supports mathmatical functions, such as addition, subtraction, multiplication, division, exponets, square root, factorial, floor, ceil, log, sin, cos, and tan. This module can also determine if a number is a prime number (is_prime), and can also convert angles to radians (angle_to_rad). Anyways, let's jump straight in to this new package!

Importing It

To import it, just put in the code below:

Or, if you want to import some individual modules, you can do the following:

Another easy option is to use the --pip install <package> version. If you don't have the --pip install command, install it first, and type the following into the Python Shell.

License

Copyright (c) 2021 CodingElf66

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringment. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contact, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in the
software.

Add

math.add is a function that helps with addition.

Parameters: num1 and num2

Returns: num1 + num2

Subtraction

math.subtract is a function that helps with subtraction.

Parameters: num1 and num2

Returns: num1 - num2

Multiply

math.multiply is a function that helps with multiplication.

Parameters: num1 and num2

Returns: num1 × num2

Divide

math.divide is a function that helps with division.

Parameters: num1 and num2

Returns: num1 ÷ num2

Exponet

math.power is a function that helps with addition.

Parameters: num1 and num2

Returns: num1 to the num2 th power

Square Root

math.root is a function that helps with square root.

Parameters: num1

Returns: The square root of num1

Factorial

math.factorial is a function that helps with factorial.

Parameters: num1

Returns: num1 !

Floor

math.floor is a function that helps with flooring numbers.

Parameters: num1

Returns: The floor of num1

Ceil

math.ceil is a function that helps with ceiling numbers.

Parameters: num1

Returns: The ceiling of num1

Cosine

math.cos is a function that helps with getting the cosine of numbers.

Parameters: radians

Returns: The cosine of radians

Sine

math.sin is a function that helps with getting the sine of numbers.

Parameters: radians

Returns: The sine of radians

Tangent

math.sin is a function that helps with getting the tangent of numbers.

Parameters: radians

Returns: The tangent of radians

Prime Numbers

math.is_prime is a function that helps with determing if a number is prime or not!

Parameters: num1

Returns: True if the number is prime, False if the number isn't prime.

Angle To Radians

math.angle_to_rad is a function that helps converting angles to radians!

Parameters: angle

Returns: The measurement of the angle in radians

Then you should be done! To import it, do the following shown below:

When you run it, you should see a folder called __pycache__, and just ignore it. If you see it, congrats! You have created your very first package, in Python!

Voters
CodingElf66
Comments
hotnewtop
MrVoo

This tutorial needs some work, for example using Pypi.

CodingElf66

@MrVoo I know, this was just meant to be a simple thing. PyPi is another huge step I can get to later, this development of packages is a first step.