Sie sind auf Seite 1von 6

Build Python Packages Without Publishing - Towards Data Science 14.06.

2020, 15:53

Build Python Packages Without


Publishing
Easily access and organize your Python modules
Max Reynolds Follow
Jun 1 · 2 min read

Photo by Leone Venter on Unsplash

Packages in Python allow for seamless distribution of python

https://towardsdatascience.com/building-a-python-package-without-publishing-e2d36c4686cd Strona 1 z 6
Build Python Packages Without Publishing - Towards Data Science 14.06.2020, 15:53

modules. When we use pip install, we are often downloading a


publicly available package from PyPI. Local packages can also
be useful for code organization and re-use, allowing you to
simply import a module without having to navigate to its
directory or re-write code. In this demonstration, we will be
building and accessing a basic Python package.

Let’s create two simple modules and package them. We will


then use this package to write a simple app that prompts a user
to select a .jpg image from their computer to be gray-scaled.

Write your modules


ui.py

#ui.py

from tkinter import filedialog


from tkinter import Tk

def get_image_file():
Tk().withdraw()
filename = filedialog.askopenfilename(title =
"Select file",filetypes = [("jpeg files","*.jpg")])
return filename

image_edits.py

#image_edits.py

https://towardsdatascience.com/building-a-python-package-without-publishing-e2d36c4686cd Strona 2 z 6
Build Python Packages Without Publishing - Towards Data Science 14.06.2020, 15:53

from cv2 import imread, COLOR_RGB2GRAY, cvtColor, imwrite


import os

def write_grayscale_image(filepath):
original_image = imread(filepath)
grayed_image = cvtColor(original_image,
COLOR_RGB2GRAY)
grayed_filename=os.path.join(os.path.split(filepath)
[0],'grayed_'+os.path.split(filepath)[1])
print(grayed_filename)
imwrite(grayed_filename, grayed_image) #export
grayscaled image
return grayed_filename

Init and Setup Files


Create a blank __init__.py Cle, which will be used by Python to
recognize this as a package.

$ touch __init__.py

At this point, our Cle structure should look like this:

dir/
image_pkg/
ui.py
image_edits.py
__init__.py

Next, create a setup.py Cle outside of your package directory

https://towardsdatascience.com/building-a-python-package-without-publishing-e2d36c4686cd Strona 3 z 6
Build Python Packages Without Publishing - Towards Data Science 14.06.2020, 15:53

setup.py

import setuptools

setuptools.setup(name='examplepackage',
version='0.1',
description='An example package',
url='#',
author='max',
install_requires=['opencv-python'],
author_email='',
packages=setuptools.find_packages(),
zip_safe=False)

Note that packages not included in the python standard library


should be included in install_requires. Our Cle structure should
now look like:

dir/
image_pkg/
ui.py
image_edits.py
__init__.py
setup.py

Build and install your package


If you are using virtual environments (generally good practice
for Python development), create and activate your
environment.

https://towardsdatascience.com/building-a-python-package-without-publishing-e2d36c4686cd Strona 4 z 6
Build Python Packages Without Publishing - Towards Data Science 14.06.2020, 15:53

$ python3 -m venv myenv


$ source myenv/bin/activate

Install wheel.

$ pip install wheel

Install the package into your environment.

$ pip install .

Our package has been created, and we can now use it from
anywhere. Let’s create a simple app which incorporates our
package contents.

main.py

#main.py

from image_pkg.ui import get_image_file


from image_pkg.image_edits import write_grayscale_image

write_grayscale_image(get_image_file())

We have now built and implemented a basic python package!

https://towardsdatascience.com/building-a-python-package-without-publishing-e2d36c4686cd Strona 5 z 6
Build Python Packages Without Publishing - Towards Data Science 14.06.2020, 15:53

More information regarding package distribution, licensing,


and installation can be found in the documentation.

Python Software Development Data Science Programming Coding

Discover Medium Make Medium Explore your


Welcome to a place where yours membership
words matter. On Medium, Follow all the topics you Thank you for being a
smart voices and original care about, and we’ll member of Medium. You
ideas take center stage - deliver the best stories for get unlimited access to
with no ads in sight. Watch you to your homepage and insightful stories from
inbox. Explore amazing thinkers and
storytellers. Browse

About Help Legal

https://towardsdatascience.com/building-a-python-package-without-publishing-e2d36c4686cd Strona 6 z 6

Das könnte Ihnen auch gefallen