python-structure
python-structure copied to clipboard
Sample project structure for a python package.
python-structure
Sample project structure for a python package, contains all the necessary files to publish a PyPI package.
Guide
- use
Python 3andpip3likenpm - use
virtualenvlikenode_modules
Tool Installation
pip3 install virtualenvwrapper
echo "
# python virtualenv
source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
Project creation
I'm assuming my PyPI package name is structure. Also, don't use non-alphabetic characters; for exampl PyPI will not allow '-' and '_'.
- create a project on Github, add
README, .gitignore, LICENSE. Clone your git repo locally. - update your
.gitignore(see example) and remove.mdfromREADME.mdcuz PyPI can't even - create a
setup.py(see example). - make a folder with your package name to contain your source code.
mkdir structurein my case. - since the folder will be a module, add
structure/__init__.py; this file will also contain the version, and the packaged methods. - add any of your Python source code under
structure.pyand import the needed module methods instructure/__init__.py - finally, at the root, do
pip3 freeze > requirements.txtto generate list of dependencies you used, so it can install withpip3 install -r requirements.txt
Packaging
Now that you have the project, it's time to publish. Navigate to the project root. First, register your package name; this may prompt you to create an account and login if it's your first time:
python setup.py register
Then, if the package name is registered successfully, upload your source to PyPI:
python setup.py sdist upload
Then wait for a bazillion years for PyPI to update it on their server before you can pip install it.
Usage
The ./index.py is a test file ignored in setup.py. A close thing to npm's node_modules is to create a virtual env to install dependencies and run code from:
# go to the root of your project folder to create env
virtualenv env
# activate the created virtual env
source env/bin/activate
# type 'deactivate' inside to quit
# install the package inside env
pip3 install -U structure
# Get ready the source code below before run:
python3 index.py
Inside ./index.py there's a sample code:
# the package
import structure
# using class
g1 = structure.greeter()
g1.greet()
# using module method
structure.hi()