<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chinmay Shah</title>
    <description>The latest articles on DEV Community by Chinmay Shah (@chinmayshah).</description>
    <link>https://dev.to/chinmayshah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F704684%2F509395f0-0fa3-4b5f-b54b-3a92191ca66c.png</url>
      <title>DEV Community: Chinmay Shah</title>
      <link>https://dev.to/chinmayshah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chinmayshah"/>
    <language>en</language>
    <item>
      <title>Writing a Python Package</title>
      <dc:creator>Chinmay Shah</dc:creator>
      <pubDate>Sat, 18 Sep 2021 11:24:39 +0000</pubDate>
      <link>https://dev.to/chinmayshah/writing-a-python-package-5302</link>
      <guid>https://dev.to/chinmayshah/writing-a-python-package-5302</guid>
      <description>&lt;p&gt;A few years back when I typed &lt;code&gt;pip install opencv-python&lt;/code&gt;, a few seconds passed and something magical happened- I was able to use OpenCV; no need to build it from source, no compiler needed, it was definitely breathtaking. I could install any package I wanted and not worry about building it source/ installing or configuring the system variable.&lt;/p&gt;

&lt;p&gt;Note: This article was originally posted on &lt;a href="https://towardsdatascience.com/writing-a-python-package-561146e53351"&gt;Medium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Over the years, I continued using pip and every time, it didn't fail to fascinate me. It really made me wonder how simple a piece of technology can be. Being a Windows user, every time I installed something new, I had to configure the system path. So this definitely made my life simpler.&lt;/p&gt;




&lt;p&gt;Files in the same module can always be imported by all files in the directory. But what if you want to make your module available throughout your system?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You add a setup.py to your module (with relevant configuration of course).&lt;br&gt;
But what if you want python package available to everyone across the globe?&lt;br&gt;
You publish your package on PyPI. (so everyone can pip install your-package-name)&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Enough talk, let's write some code. Let's write a simple function and package it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# hello.py
def heythere():
  print("hey there")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# setup.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
setup(
  author="Chinmay Shah",
  author_email='chinmayshah3899@gmail.com',
  classifiers=[
    'License :: OSI Approved :: MIT License',
    'Programming Language :: Python :: 3.7',
  ],
  description="Says hello",
  license="MIT license",
  include_package_data=True,
  name='hello',
  version='0.1.0',
  zip_safe=False,
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Setup.py&lt;/code&gt; is what &lt;code&gt;pip&lt;/code&gt; looks for in a given directory. It uses something called setuptools[1] which enables packaging. It contains the name of your package, a brief description of your package, along with author information. And don't fail to mention which python version it's made for. All of this metadata is important.&lt;/p&gt;

&lt;p&gt;Looks simple? Let's try this stuff out then. Let's install it - &lt;code&gt;pip install .&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Installing your python package&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ofDVW09M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5rm4ncvxfo56lc9dd1x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ofDVW09M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5rm4ncvxfo56lc9dd1x.png" alt="Installing your python package"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But what does it mean when I say it installs it?&lt;br&gt;
First, it creates a wheel (&lt;code&gt;.whl&lt;/code&gt;) file; which is an accepted file for package distribution.&lt;br&gt;
In the installation process, it uses this wheel file and installs it in site-package directory(Anaconda uses this).&lt;br&gt;
In-case of downloading it from the internet, a local cache is often created in &lt;code&gt;pkgs&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Running the installed module&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zflddOwv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wezi22km2cveoiigeic9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zflddOwv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wezi22km2cveoiigeic9.png" alt="Running the installed module&amp;lt;br&amp;gt;
"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;But what is &lt;code&gt;pip&lt;/code&gt;?&lt;br&gt;
pip is a package installer for Python used to install packages (mainly) from PyPI(Python Package Index). Launched in 2008 as an upgrade to &lt;code&gt;easyinstall&lt;/code&gt;, even though both are built on top of setuptools. [1]&lt;br&gt;
PyPI is a vast package index where anyone can submit their package, and anyone across the globe can do pip install your-package-name .&lt;br&gt;
Look out for the next post, where I'll be covering on how to write package as well as publishing it on PyPI.&lt;/p&gt;




&lt;p&gt;Have any thoughts? Reach out on &lt;a href="https://twitter.com/chinmayshah899"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/chinmayshah99/"&gt;Linkedin&lt;/a&gt; or &lt;a href="//mailto:hi@chinmayshah.xyz"&gt;E-Mail&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Reference:&lt;br&gt;
[1] Setuptools - &lt;a href="https://setuptools.readthedocs.io/en/latest/"&gt;https://setuptools.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
