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 myFirstPiP
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.
/myFirstPiPy
/myFirstPiPy
__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.6"
- "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:
- pytest
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.1.tar.gz
.
File metadata
- Download URL: myFirstPiPy-0.0.1.tar.gz
- Upload date:
- Size: 3.3 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 | 721340a142c8e558758e214d9997b157b557dcdc7c455781eb37895c5e2a0c68 |
|
MD5 | a157cacb2145162db4c382342af17bb8 |
|
BLAKE2b-256 | 2ca5a186b6ba53d69c4afce5c1a0d30193b8ad84c42ec4c0a2ba011ad80f3ee5 |
File details
Details for the file myFirstPiPy-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: myFirstPiPy-0.0.1-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 | 1ea25bc29556b691a4c76972609c7660fbff860605176e6f02db45ee5d3329a9 |
|
MD5 | 11b640b636a4628b41d3f60b869e745c |
|
BLAKE2b-256 | 55adbbeaeb31e97d75652588b4689e514f4364f7c23d0d0b76232370a71a496a |