Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
  • entries
    3
  • comments
    0
  • views
    87

Packaging for PyPI

SafyreLyons-5LT

59 views

How to PROPERLY Make and Package a PyPI module

First thing is first: Python

This is a case you are making a PYTHON module and unless you WANT to go into the nitty gritty of compilers and binaries for lets say JavaScript, stick with Python files. 

 

Proper folder-ing

This is how your folder should be set up (with files

Quote

Main Folder (Name DOESN'T MATTER)
|
| - src/

|        |
|        | - your_package.py
|
| - LICENCE
| - README.md
|- setup.py

 

Creating the actual functionality of the package

Creating the package just requires code that does SOMETHING. Here I will just use a simple one.

def switch(x):
    x_a = x.lower().replace('e', 'a')
    print(x_a)

Name the file what you wanna call your package! This one of mine is called e so I will call it e.py

 

Next thing you wanna do before adding everything is to test your package. Make app.py and do

import your_pkg

your_pkg.fucntion("whatever you want to do with this")

My example of e.py would be the following

import e

e.switch("Hello everyone! Welcome to the ever-lasting e-party where we try to use e")

Now delete app.py from your folder. We will now set up the folder to look as it does in Proper folder-ing

You might also get a folder called __pycashe__, delete that too.

For those who did not understand the text version here is an image of what you should see
image.png.a56a4126e1af85b5322f1d39c128cf46.png

e.py being named what you named your package.

 

NOTE: I misspelled "setup" and did not notice until running the command in Command Time, so don't be like me and spell it RIGHT
NOTE2: I upper cased the MD in README.MD, don't do that, do README.md

Now sign up for https://pypi.org/ (this is Pythons repo for the modules (like NodeJS's npmjs.com))

 

Remember those login details for later.

 

Setup the bakery (get it because Py sounds like pie)

Now set your setup.py file like this:

from setuptools import setup
with open("README.md", "r") as fh:
    long_description = fh.read()

setup(name='your_pkg',
      version='1.0.0',
      description='describe your package',
      long_description=long_description,
      long_description_content_type="text/markdown",
      author='Your Name',
      author_email='your@email.tld',
      license='your_licence',
      py_modules=['your_pkg'],
      package_dir={'': 'src'},
      zip_safe=False)

My e example would look like this:

from setuptools import setup
with open("README.md", "r") as fh:
    long_description = fh.read()

setup(name='e_switch',
      version='1.0.0',
      description='hallo world this is my packaga for "e" tha packaga that switchas all "e"s with "a"s',
      long_description=long_description,
      long_description_content_type="text/markdown",
      author='Safyre Lyons',
      author_email='lyons.safyre@gmail.com',
      license='MIT',
      py_modules=['e_switch'],
      package_dir={'': 'src'},
      zip_safe=False)

Feel free to copy/paste it, just change the needed things.


ALSO!! Make sure you look up whether your package name was taken. e was taken.

FINAL PREP: README.md

README data is essential for ALL projects. Here you just need to include: How to install (if you plan on sharing the source code on GitHub or something), how to use.

 

README.md should be like my example:

# E
Turn e's into a's!!

## Installation:
`pip install e_switch`

## Usage:
```python
import e_switch as e

e.switch("eeee look at meeee")
```
#### Output:
aaaa look at maaaa

and print out as

image.png.d18fc3d75462eb65949b19d119ae3c10.png

 

Command Time!!

Well it's the hardest part. Command time.

NOW!! My examples might look different from how you do it.
I have both Python2 and Python3, so my terminal has to do "python3 somrthofbwibfewibfiub.py" for any code in Python3, setup.py was written for Python3. If you don't have Python2 set as "python" then when it says "python3" type out "python".

Setup dist
image.png.58a0859d8464101658b15295ece810ec.png

This will create a folder called dist/ it will be important for our next command.

Do:

image.png.002fc6b4ba66a0594d2cf1a5e8fc366e.png

and follow the prompts

 

image.png.dc2dcd2c73debbaffdddaf4eafddf3b1.png

 

BRAG!!!

Well, ya did it. If you did everything RIGHT, you should end up with a working package

Go to https://pypi.org/manage/projects/ and make sure you are logged in
image.png.240e583ebe6280ce0394bfbda5c5217e.png

Look at the top there, (it will not be outlined in red on your screen) but it is THERE!!! IT EXISTS!!!

You win!



0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×