Skip to content

Instantly share code, notes, and snippets.

@pouyaardehkhani
Last active February 20, 2025 22:08
Show Gist options
  • Save pouyaardehkhani/dc483e8bbfec2dc25b4725a1411c7b39 to your computer and use it in GitHub Desktop.
Save pouyaardehkhani/dc483e8bbfec2dc25b4725a1411c7b39 to your computer and use it in GitHub Desktop.
How to create a python package and publish it to pypi in PyCharm

How to create a python package and publish it to pypi in PyCharm

Setup a new python project that we will package later

I typically use pycharm and create a new virtual environment (under the folder ‘venv’ – see below) that I use as the interpreter for that project:

  • After you create a new project on pycharm with Name of project =
  • Create a new package and name it.
  • Create a setup.py file outside of your package folder.
  • Create a version.py file in your package folder then write this in it __version__ = "your desired verstion" .
  • Write this in init.py file in your package from .version import __version__.
  • Create a README.md file for describing your package outside of the package folder.
  • Create a MANIFEST.in file.(for now it's empty)
  • You will also need an LICENSE file outside of the package folder. (For License you can use MIT-GNU-BSD-...)

The folder structure should look like below:

/name_of_project
  /venv
  /name_of_package
    __init__.py
    my_code1.py
    my_code2.py
    version.py
  setup.py
  README.md
  LICENSE
  MANIFEST.in

Paste the following into setup.py

import os.path

from setuptools import find_packages, setup


def read(rel_path: str) -> str:
    here = os.path.abspath(os.path.dirname(__file__))
    # intentionally *not* adding an encoding option to open
    with open(os.path.join(here, rel_path)) as fp:
        return fp.read()


def get_version(rel_path: str) -> str:
    for line in read(rel_path).splitlines():
        if line.startswith("__version__"):
            # __version__ = "0.9"
            delim = '"' if '"' in line else "'"
            return line.split(delim)[1]
    raise RuntimeError("Unable to find version string.")


this_directory = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(this_directory, "README.md"), encoding="utf-8") as f:
    long_description = f.read()

setup(
    name="Name_of_the_package",
    version=get_version("folder_that_contaions/version.py"),
    description="Discription of the package",
    packages=find_packages(),
    long_description=long_description,
    long_description_content_type="text/markdown",
    classifiers=[
        "Development Status :: 4 - Beta",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3 :: Only",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "License :: OSI Approved :: MIT License",
        "Intended Audience :: Developers",
        "Intended Audience :: Education",
        "Intended Audience :: Science/Research",
        "Topic :: Scientific/Engineering",
        "Topic :: Scientific/Engineering :: Artificial Intelligence",
        "Topic :: Software Development",
        "Topic :: Software Development :: Libraries",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Topic :: Scientific/Engineering :: Mathematics",
    ],
    url="url of the repository(mostly we use github repo url)",
    author="name",
    author_email="email of the author",
    install_requires=[
        "tensorflow >= 2.9.1",
        "numpy",
        "keras",
    ],
    extras_require={
        "dev": [
            "check-manifest",
            "twine",
            "black",
        ],
    },
)

Let's dive into it:

  • For changing the package name edit this name="Name_of_the_package".
  • Edit this version=get_version("folder_that_contaions/version.py") to version=get_version("name_of_package/version.py").
  • Write your name in here author="name", and your email here author_email="email of the author",.
  • For url you can use the github repo url here url="url of the repository(mostly we use github repo url)",.
  • classifiers are like tags.
  • Add description of your package here description="Discription of the package",.
  • In install_requires = [ write every package that your code needs in order to run properly.

Editing the MANIFEST.in

This file tells any resources that we want to include in our package.

include LICENSE
recursive-include name_of_package *.py

Above code indicates that all of the .py files in name_of_package folder and LICENSE file to be included in the package.

Creating tar.gz file of our repo in PyCharm

Go to Tools->Run setup.py tsks->sdist ENTER

This will create two folders:

  1. dist

  2. name_of_package.egg-info

Checkout the dict folder, it will contain the package nicely compressed!

Install the newly created package from local project:

Open Terminal in PyCharm and copy this in it:

pip install package_name --no-index --find-links file://C:\Users\User_name\PycharmProjects\name_of_project\dist

Upload the dist\ files in testpypi

Before uploading to pypi we must test it to see will it works or not.

  • First we needs to pip install twine.
  • Go to testpypi and register.

Open Terminal in PyCharm and copy this in it:

python -m twine upload --repository testpypi dist/*

After you press Enter it will asks for username and password.

once it's done test the installation from testpypi

Upload the dist\ files in pypi

Once everything is fine then write this code in the PyCharm Terminal:

python -m twine upload --repository pypi dist/*

After you press Enter it will asks for username and password.

Finished

Once you upload it then you can see it in your projects and you can use the pip installation from now on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment