DEV Community

Cover image for Create package using Poetry, and deploy on private PyPi Server
Oktapian Candra
Oktapian Candra

Posted on

Create package using Poetry, and deploy on private PyPi Server

Ah, Python developers—masters of their craft, wielders of virtual environments, and occasional snackers of leftover pizza at 3 a.m. If you’ve ever wanted to create your own private Python package (because who doesn’t want to feel like a package-deploying secret agent?), this article will guide you through the process using Poetry and pypiserver. By the end, you’ll not only have a private package but also a sense of accomplishment rivaled only by successfully debugging a KeyError at first glance.

What is Poetry? (Not the Rhyming Kind)

Before you start imagining yourself as a wordsmith crafting verses under the moonlight, let me clarify: Poetry in this context is a tool for managing Python projects. It simplifies dependency management, package versioning, and publishing. Think of it as your Python project’s personal assistant—organizing everything neatly so you can focus on writing code (and googling stack traces).

Key Features of Poetry:

  • Dependency Management: Specify, resolve, and manage your project dependencies in one place.
  • Virtual Environment Integration: Automatically creates virtual environments for your project.
  • Packaging Made Easy: Build and publish Python packages with minimal effort—no need to wrestle with setup.py.

In short, Poetry is like that one coworker who always keeps their desk tidy and remembers everyone’s birthday.

What is pypiserver? (Hint: It’s Not a Fancy Dessert)

Now, let’s talk about pypiserver: a simple, lightweight PyPI (Python Package Index) server you can host yourself. Why would you want this? Well, maybe you’re building proprietary software, or maybe you just want to feel like a Python supervillain hoarding private packages. Either way, pypiserver lets you securely host and serve your own packages without relying on public repositories like PyPI.

Key Features of pypiserver:

  • Private Package Hosting: Store your own packages locally or on a private server.
  • Lightweight: Minimal setup and resource usage, perfect for small-scale hosting.
  • Compatibility: Works seamlessly with pip and other Python tools.

Essentially, it’s like running your own private PyPI, but without needing a degree in server management.

Why Would You Want a Private Package Anyway?

Good question! Here are some common reasons:

  • You have proprietary code you don’t want to share with the world.
  • You’re building reusable modules for internal use within your organization.
  • You just want to flex on your teammates by saying, “Oh, I have my own PyPI server.”

Now that we’ve got the basics down, let’s jump into the fun part: creating a private package and deploying it on pypiserver.


Step 1: Install Poetry

First, install Poetry. If you don’t already have it, run the following command:

curl -sSL https://install.python-poetry.org | python3 -
Enter fullscreen mode Exit fullscreen mode

After installation, make sure Poetry is accessible by checking its version:

poetry --version
Enter fullscreen mode Exit fullscreen mode

If you see something like Poetry version 1.6.0, congratulations—you’re ready to roll. If not, double-check your installation steps, and try not to curse too loudly.


Step 2: Create Your Python Package

Let’s create that private package. For this example, we’re going to make a package called my_private_package because creativity is overrated. Run the following:

poetry new my_private_package
Enter fullscreen mode Exit fullscreen mode

This command creates a new directory with the following structure:

my_private_package/  
├── my_private_package/  
│   └── __init__.py  
├── tests/  
│   └── __init__.py  
├── pyproject.toml  
Enter fullscreen mode Exit fullscreen mode
  • The pyproject.toml file is Poetry’s version of a project blueprint. It defines everything about your project, from its dependencies to its version number.
  • The tests/ directory is where you can write tests… you know, once you stop procrastinating.

Step 3: Write Some Code

Now, let’s add some code to your package. Open my_private_package/__init__.py and add the following:

def greet(name: str) -> str:  
    return f"Hello, {name}! Welcome to your private package."  
Enter fullscreen mode Exit fullscreen mode

It’s simple, but hey, it gets the job done.


Step 4: Build the Package

To build your package, navigate to the root directory of your project and run:

poetry build
Enter fullscreen mode Exit fullscreen mode

This will create a dist/ directory containing .tar.gz and .whl files for your package. These are the files you’ll upload to your private PyPI server (or, as I like to call it, your “Python treasure chest”).


Step 5: Set Up pypiserver

Install pypiserver

Now it’s time to set up your private PyPI server. First, install pypiserver:

pip install pypiserver
Enter fullscreen mode Exit fullscreen mode

Create a Directory for Your Packages

Decide where you want to store your private packages. For example:

mkdir ~/private_packages
Enter fullscreen mode Exit fullscreen mode

Add security using htpasswd

Create a user and choose a beautiful password for your private PyPi Server

htpasswd -sc ~/private_packages/htpasswd.txt admin
Enter fullscreen mode Exit fullscreen mode

Start the Server

Run the following command to start pypiserver:

pypi-server run -p 8060 ~/private-packages -P ~/private-packages/htpasswd.txt --hash-algo=sha256
Enter fullscreen mode Exit fullscreen mode

This will start the server on http://localhost:8060. You can now upload and serve packages from this directory.


Step 6: Deploy Your Package to pypiserver

Let’s back to my_private_packageproject directory, and add this on your private PyPi Server (let name it as “treasure_chest”) to the pyproject.toml:

[[tool.poetry.source]]  
name = "treasure_chest"  
url = "http://localhost:8060/simple/"  
priority = "supplemental"
Enter fullscreen mode Exit fullscreen mode

To upload packages to a server with Poetry, you must first configure it.

poetry config repositories.treasure_chest http://localhost:8060/  
poetry config http-basic.treasure_chest admin "<your_beautiful_password>"
Enter fullscreen mode Exit fullscreen mode

Then, yeah this time for upload your packages using this command:

poetry publish --repository treasure_chest
Enter fullscreen mode Exit fullscreen mode

This will upload your package on dist/to your private PyPi Server, and you will see your packages on http://localhost:8060/simple/.


Step 7: Install Your Private Package

Using pip install

To install your private package from pypiserver, use pip with the --extra-index-url option:

pip install --extra-index-url http://localhost:8080 my_private_package
Enter fullscreen mode Exit fullscreen mode

Using poetry

Or.. you can install it using poetry, with add your private PyPi server configuration on your pyproject.toml like in the package directory earlier

[[tool.poetry.source]]  
name = "treasure_chest"  
url = "http://localhost:8060/simple/"  
priority = "supplemental"
Enter fullscreen mode Exit fullscreen mode

Then running this command

poetry add --source treasure_chest my_private_package
Enter fullscreen mode Exit fullscreen mode

If you see Successfully installed my_private_package, congratulations—you’ve officially entered the realm of private package ninjas.


Wrapping Up

And there you have it! You’ve created a private Python package using Poetry and deployed it on pypiserver. Whether you’re building an empire of proprietary modules or just trying to impress your developer friends, you now have the tools to do it.

Remember, with great power comes great responsibility. Use your private PyPI server wisely, and don’t forget to share this tutorial with your favorite Python buddies. Or don’t—it’s your secret package lair, after all.

Top comments (0)