# Publish your own Python Package

Hello folks, 
You might have seen some of my packages in the previous [blog](https://blogs.lakshaykumar.tech/computer-vision-made-easy)
While working on some project / solving any programming problem, have you developed a new algorithm or utility that can help developers in a more efficient way?
So this blog is for you! This blog will discuss **How can you publish your own python package?**
So Let's get started.

There are few terms you should be familiar with -

- [`class`](https://www.geeksforgeeks.org/python-classes-and-objects/) : Blueprint of object. Object is simply a collection of variables and functions.
- [`self`](https://www.geeksforgeeks.org/self-in-python-class/) : Python keyword, a parameter refers to the current instance of the class.
- [`__init__()`](https://www.geeksforgeeks.org/__init__-in-python/) : A function that runs by default whenever a class is executed.
- [`import`](https://docs.python.org/3/reference/import.html) : Keyword to include package in our program

The links mentioned along with the keywords will help you know more, I have added a summary for your reference above.

In this blog, we will **create a simple package that can add numbers from any data structure**. I'll be discussing the following ways of doing it.
- Direct Function Implementation
- Class Usage
- Through Function of Class
- Through the external class of package

We are going to publish our package on [pypi.org](https://pypi.org/) - A popular collection of all the libraries. First, create an account here and save the login credentials.

Now let's set up a project. Make sure your project name matches the name of the package you intend to publish. I'll be using the different package name for each way I mentioned above.

### Direct Function Implementation
Project Name: **addition_by_direct_function_implementation**
Inside this project, create one more subfolder with the same name and one python file `main.py`. Inside the subfolder create one python file `__init__.py`

**Explanation**: We will test our code in `main.py`, `__init__.py` is the python script that will run by default when we call our package from the project folder.

Your file structure should look something like this.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667487588133/oJSIJ8HR7.png align="left")

**`__init__.py`**
Here we have simply defined a function `addnumbers` with the numbers as parameter. The function returns the sum. Here's the code
```
def addnumbers(numbersdatastructure):
    add=0
    for i in numbersdatastructure:
        add+=i
    return add
```

Now our function-based package is ready, let's test on our local system.
`main.py`
```
from addition_by_direct_function_implementation import addnumbers

sum = addnumbers([1,2,3,4,5,6,7,8,9])

print(sum)
```

Since we have a subfolder with the name **addition_by_direct_function_implementation** its calling that folder and by default `__init__.py` gets called, its referring that code. Hence its calling function `addnumbers()` from `__init__.py` of `addition_by_direct_function_implementation` sub folder.



### Class Usage
Project Name: **addition_by_class_usage**
Inside this project, create one more subfolder with the same name and one python file `main.py`. Inside the subfolder create one python file `__init__.py`

**Explanation**: We will test our code in `main.py`, `__init__.py` is the python script that will run by default when we call our package from the project folder.

Your file structure should look something like this.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667488558059/mQ6x9s1mI.png align="left")

**`__init__.py`**: Writing a class and within that class, we are creating 2 functions for multiplying and adding the numbers in the data structure.
```
class addition():

    def addthenumbers(self,numbersdatastructure):
        self.numbersdatastructure = numbersdatastructure
        add=0
        for i in self.numbersdatastructure:
            add+=i
        return add

    def multiplythenumbers(self,numbersdatastructure):
        self.numbersdatastructure = numbersdatastructure
        mul=1
        for i in self.numbersdatastructure:
            mul*=i
        return mul
```
Here the self keyword is referring to the variable of current function.

Now our function-based package is ready, let's test on our local system.
`main.py`

```
import addition_by_class_usage as acu
objectOfClass = acu.addition()
addition = objectOfClass.addthenumbers([1,2,3,4,5])
multiply = objectOfClass.multiplythenumbers([1,2,3,4,5])
print(addition,"\n",multiply)
```

First I am importing the packing (here package name is project folder name with `__init__.py` and to make its usage easy in the code, I'll call it as `acu`.
Then I am creating object of class `acu` and then calling then simply functions


### Through Function of Class

Project Name: **functions_in_class**
Inside this project, create one more subfolder with the same name and one python file `main.py`. Inside the subfolder create one python file `__init__.py`

**Explanation**: We will test our code in `main.py`, `__init__.py` is the python script that will run by default when we call our package from the project folder.

Your file structure should look something like this.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667493012116/e-Y9E1WQh.png align="left")

`__init__.py`
```

class addition():

    def addthenumbers(self,numbersdatastructure):
        self.numbersdatastructure = numbersdatastructure
        add=0
        for i in self.numbersdatastructure:
            add+=i
        return add

    def multiplythenumbers(self,numbersdatastructure):
        self.numbersdatastructure = numbersdatastructure
        mul=1
        for i in self.numbersdatastructure:
            mul*=i
        return mul


def main():
    print("Main function called")


if __name__ == "__main__":
    main()
```


Here we have defined additional function `main()` and in that function I am just printing a statement.

`main.py`

```
import functions_in_class as acu
from functions_in_class import main

objectOfClass = acu.addition()

addition = objectOfClass.addthenumbers([1,2,3,4,5])
multiply = objectOfClass.multiplythenumbers([1,2,3,4,5])
print(addition,"\n",multiply)
main()
```
Now we are calling the function `main()` from the class `functions_in_class`. Here it will ignore the class and directly call the function.

### Through the external class of package
Here the import syntax is just like direct function implementation, but instead of function, we are calling a python file that have the code.
For example 
```
from packagename import classname
```
Here packagename is the name of folder and classname is another python file in the folder other than `__init__.py`

For any queries, feel free to ask in comments or contact me over [email](mailto:contact@lakshaykumar.tech)


### Publishing Python Package
So now, we are ready with our package, lets publish. We'll be publishing on test pypi.org because this is only for testing purposes.

**Create** Liscence.txt file : Refrence : [https://choosealicense.com/](https://choosealicense.com/)<br>
**Create** README.md : Reference : [https://dillinger.io/](https://dillinger.io/)
<br>
**Create setup.py** and add the following code. Replace with your own details
Reference : geekforgeeks.org

```
import setuptools

with open("README.md", "r") as fh:
	long_description = fh.read()

setuptools.setup(
	# Here is the module name.
	name="addition_by_class_usage",

	# version of the module
	version="0.0.1",

	# Name of Author
	author="Lakshay Kumar",

	# your Email address
	author_email="contact@lakshaykumar.tech",

	# #Small Description about module
	# description="adding number",

	# long_description=long_description,

	# Specifying that we are using markdown file for description
	long_description=long_description,
	long_description_content_type="text/markdown",

	


	# if module has dependencies i.e. if your package rely on other package at pypi.org
	# then you must add there, in order to download every requirement of package
	 install_requires=[ ],
#Write packages that needs to be preinstalled for using this package.


	license="MIT",

	# classifiers like program is suitable for python3, just leave as it is.
	classifiers=[
		"Programming Language :: Python :: 3",
		"License :: OSI Approved :: MIT License",
		"Operating System :: OS Independent",
	],
)
```


Your folder structure should look like this.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667495962959/SMPzQLij8.png align="left")

Now to install a package called twine using this line -
```
pip install twine
```

Then
```
python setup.py bdist_wheel 
```

You'll see more folders like this

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667496056540/etZViihMb.png align="left")

Upload to test env.
```
twine upload -r testpypi dist/*
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667496153718/TtM-GjccW.png align="left")

Woohoo!
You finally uploaded it to the test environment. To upload in production environment, use this command
```
twine upload dist/* 
```

You'll see your project link at the bottom

### Test
To test, go to project link and copy the pip command. In my case its - <br>
```pip install -i https://test.pypi.org/simple/ addition-by-class-usage```

And install in your project.

### Update
To update make changes in your code and **It is important to make changes in the version number**. Then you can use the build and upload again to make changes.

That's it. For any queries, feel free to comment or contact me over email.
Don't forget to subscribe on my website - https://www.lakshaykumar.tech/








