Hello World!
Python has a simple way of creating and publishing packages. Python Package Index is a package repository which maintains all the published package.
Once I was curious about that packaging thing in python, So decided to learn this stuff. Along with that learning journey, I maintained a note and wrote necessary steps. I refined that note and posted here so that it may help someone who is looking into it. If you want to learn this stuff, this post can help you. You need a PyPI account to publish a package, So before starting, please Sign Up on PyPI, then proceed with the below process.
Create a directory for the package.
mkdir demo_car
Create file
__init__.py
in the directory.
touch demo_car/__init__.py
(This file represents that the directory which contains it is a python package.)Write
'name = "<package-name>"'
in file.
echo 'name="demo_car"' >> demo_car/__init__.py
Create python scripts for the package.
touch demo_car/engine.py
Let's write some code in engine.py script. Open an editor (I prefer Nano), write some python code or paste below sample code and save the file by Ctrl+o.
nano demo_car/engine.py
def start():
print("Started!")
def stop():
print("Stopped!")
~~~~
> If no editor is available then install nano with the following commands.
> `apt-get update`
> `apt-get install nano`
> `export EDITOR=nano`
- Package files structure should be like this.
.
├── demo_car
│ ├── engine.py
│ └── init.py
├── LICENSE
├── README.md
└── setup.py
~~~~
-
Purpose of these files.
-
setup.py
Build script for packaging. -
LICENSE
Every package should have a License. We use MIT license here. -
README.md
This file contains a description of the package which will be shown on PyPI repository.
-
Create these three files.
touch setup.py LICENSE README.md
-
Open these files in the editor and update them.
- For setup.py file
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="demo_car",
version="0.0.1",
author="Umesh Kumar Dhakar",
author_email="kumar886umesh@gmail.com",
description="A demo_car package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/umeshdhakar/sample-python-package",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
~~~~
- For LICENSE file
MIT License
Copyright (c) [2019] [demo_car]
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.
~~~~
- For README
## Demo_car
This is a sample package to learn the steps of creating and publishing package.
~~~~
- Now we are ready with all the necessary files so without waiting we are going to packaging it.
- Install virtual environment package.
*(If you already have then skip this step.)*
`pip install virtualenv`
- Create and activate the environment.
`virtualenv -p python3 .`
`source bin/activate`
- Install or Update the setuptools and wheel packages in the environment.
`pip install --upgrade setuptools wheel`
*setuptools and wheel are the packages which use setup.py file to build
package.*
Now all Set! 😊, You are going to create the package, Are you excited? 😉
- Fire below command to build package.
`python3 setup.py sdist bdist_wheel`
Hopefully, the package should be built and you can see package's compressed file in `dist` directory beside setup.py file.
Well Done my friend(👍), You have created It.
***
**Installing the created package.**
- Create a new environment at some other location and activate it.
*(Now you know how to do this).*
- Copy that zip file in the new environment.
`pip install <package>`
(`pip install demo-car-0.1.tar.gz`)
*Run* `pip list` *to show the installed package in the activated environment. Your package should be enlisted here.*
Congratulations! (😊) You have installed your package.
***
**It's time to publish the Package to PyPI so that it is publically available.**
- We need to install twine package to upload it but before that go to the path where `setup.py` exists and Activate the first environment you created for packaging then fire up the command to install or update twine.
`pip install --upgrade twine`
- Final command to send it to **PyPI**.
`twine upload dist/*`
It will prompt you for the account credentials,
So enter your username and password, then it will start uploading your package.
**Congratulations! (🎉 ) Dear, You did it.**
Now your package should be available on the [PyPI](https://pypi.org/)
([Reference]( https://packaging.python.org/tutorials/packaging-projects/))
Thank you for reading 😊.
These are the minimum steps to create a package which I know. Please refer official documentation if you want to explore more 💡.
Feel free to ask query, I am happy to help. Have you found any useful information which I missed here? then please comment below.
Top comments (1)
Just joining the python world and you gave me some insight how packages work there.
Thanks