Write in your project the following directory structure [6] [7] [13]
setup.py
README.md
src/
my_package/
__init__.py
module1/
__init__.py
module1.py
module2/
__init__.py
module2.py
tests/
test_module1.py
First init.py should look like this: [This is on a pep]
__version__ = '0.1.2'
import .module1 # only if you want the to be always imported when doing
import .module2 # import my_package, this is slow and inefficient [10]
__all__ = ['module1', 'module2'] # from my_package import *
Setup.py should look like this [1] [6]:
from setuptools import setup, find_packages
setup(
name="my_package",
version="0.1",
install_requires= ["subprocess32;python_version<'3.0'",
] ******
packages=find_packages("src"), # find_namespace_packages only works on
# python3
package_dir={"": "src"}
entry_points={
"console_scripts": [
"script1 = my_package.module1:main_func",
"script2 = my_package.module2:some_func",
],
"gui_scripts": [
"script3 = my_package_gui:start_func",
]
}
)
Use:
from __future__ import print_function
At the beginning of files to force python3 print styles in python2 code [11]
Importing different packages depeding on python version [14]:
import sys
if sys.version_info[0] == 3:
from importlib import abc
else:
from importlib2 import abc
Or:
try:
from importlib import abc
except ImportError:
from importlib2 import abc
Generate source distribution (with wheels): [1]
setup.py sdist bdist_wheel
python setup.py bdist_wheel --universal # if it runs pure python2/3 code
For development: (uses symlinks, then reinstalling is not necessary anymore while changing and testing code):
python3 setup.py develop --prefix=/home/memeruiz/local/DIR/test
Instead of using xstow, you could use pip for development like this:
pip install -e <path> --no-deps [2]
Upload stuff to pypi (must make an account on pypi): [3]
twine upload dist/*
python3 -m venv <DIR> # python3
source <DIR>/bin/activate
virtualenv <DIR> # python2/3
source <DIR>/bin/activate
Install from requirement files: [5]
pip install -r requirements.txt
[1] https://packaging.python.org/guides/distributing-packages-using-setuptools/#distributing-packages
[2] https://packaging.python.org/tutorials/installing-packages/
[3] https://packaging.python.org/guides/distributing-packages-using-setuptools/#id78
[4] https://packaging.python.org/tutorials/installing-packages/#installing-requirements
[5] https://pip.pypa.io/en/latest/user_guide/#requirements-files
[6] https://packaging.python.org/tutorials/packaging-projects/
[7] https://docs.python.org/3/tutorial/modules.html#packages
[8] https://packaging.python.org/overview/
[9] https://packaging.python.org/
[10] https://stackoverflow.com/questions/35727134/module-imports-and-init-py-in-python
[12] https://pypi.org/project/future/
[13] https://pythonhosted.org/an_example_pypi_project/setuptools.html
[14] https://docs.python.org/3/howto/pyporting.html