My first PiP
Project description
myFirstPiPy
Ignore this package ! It is simple showing progress of learning developing reusable python modules
Steps to create your own PIP Module
Each file extended by .py
becomes a python module as follows:
#!/usr/bin/python
def myMethod():
msg = "Hello PiP"
print ("MSG: {}".format(msg))
return msg
myVariable = 123
If you want to use your file as a module you simple have to import it:
#!/usr/bin/python
import myModule
myModule.myMethod()
Also possible:
#!/usr/bin/python
from myModule import myMethod, myVariable
print myVariable
#Share your PiP
To share your module as a PIP Package everyone can install by pip install yourPackageName
requires a bit more structure.
After registering on PyPi.org (and the Test Space accont!) install an utility for publishing Python packages on PyPi:
pip install twine
Setup.py
Create a directory structure as follows and add plain files as shown:
/myFirstPiPy
/myFirstPiPy
__init__.py
setup.py
LICENSE
README.md
Within the __init__.py
you can add python code to be executed while importing the module.
The setup.py
requires a fixed structure you can copy and past here for customizing your needs:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="myFirstPiPy",
version="0.0.1",
author="Daniel Bunzendahl",
author_email="StudentDanBu@gmail.com",
description="My first PiP",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/StudentESE/myFirstPiP",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
Note: You have to create a new Git Repository here and copy the URL
To generate the wheel for native compilations and tar.gz
for python module - just do:
cd myFirstPiPy
python setup.py sdist bdist_wheel
generates:
dist/
myFirstPiPy-0.0.1-py-none-any.whl
myFirstPiPy-0.0.1.tar.gz
Testdrive and Upload
First we test how a submission would look like:
twine upload --skip-existing --repository-url https://test.pypi.org/legacy/ dist/*
If the result looks good for you it is time to run
twine upload dist/*
Which will publish your first PiP Package.
Note: If you like to change files on Pypi, you need to change the version in setup.py
. It is not possible to delete the project and reupload your local version (security reason!!)
Testing
Now we like to go more professional and add unittests. So we add a directory where to add tests in multiple files.
/myFirstPiP
/myFirstPiP
__init__.py
/tests
test_var.py
test_method.py
setup.py
LICENSE
README.md
All this files are executed by the command python -m unittest discover -s ./tests -p "test_*"
Writing Tests
Tests are Classes which subclasses unittest.TestCase
. Each Method named test*
within such a class will be run. The following example shows how test_var.py
or test_method.py
could look like merged within one file.
import unittest
import myModule
class TestMyMethod(unittest.TestCase):
def test_myMethod(self):
self.assertEqual(myModule.myMethod(), "Hello PiP")
def test_myVariable(self):
self.assertIs(myModule.myVariable, 123)
if __name__ == '__main__':
unittest.main()
#Continuous Integration (CI)
A very interesting solution is Travis which runs tests on Github. To enable Travis CI you need a .travis.yml
after generating a requirements.txt
from your virtualenv
.
##Virtual Environment
First we need a virtual environment with only pip packages installed are realy neccessary. Based on this the generating of the requirements.txt
is created.
cd .. # below ./myFirstPiP
virtualenv myFirstModuleDependencies
source myFirstModuleDependencies/bin/activate
cd myFirstPiP
Install required packages and generate the requirements.txt
pip freeze > requirements.txt
##Travis
Now we can write the .travis.yml
as follows:
language: python
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.5-dev" # 3.5 development branch
- "3.6"
- "3.6-dev" # 3.6 development branch
- "3.7-dev" # 3.7 development branch
# command to install dependencies
install:
- pip install -r requirements.txt
# command to run tests
script:
- python -m unittest discover -s ./tests -p "test_*"
Git
cd ..
git pull
git add .
git commit -m "Travis CI Testdrive"
git push origin master
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file myFirstPiPy-0.0.2.tar.gz
.
File metadata
- Download URL: myFirstPiPy-0.0.2.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.6.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8180c0283763d693d1845eb0de1248ef1059375bf3f718ca45772accfb730d09 |
|
MD5 | 4eb7f3d7bfc89aff0e143ea51b1b1dff |
|
BLAKE2b-256 | 6e8b5b0d228afd389cb29c851696d0ecbfd40536f677ab739d1fc177924d651c |
File details
Details for the file myFirstPiPy-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: myFirstPiPy-0.0.2-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.6.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3de2048201ea61cb3e1bf08a4535993a68e3c42454d78ec68f5ac7861e3c09e |
|
MD5 | 8b7b20af4c76e9b81cf1c568cb260cff |
|
BLAKE2b-256 | 3f2b4dd5245f40a4ae70f4b98d95216ede869cd81d3f4bdd57e0d1bee8c490b7 |