Editing
Python Packaging
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Configuration == === Setup.py === A setup config file, `setup.py`, in the repo root directory should contain a single call to `setuptools.setup()`, like so: <ref>[https://python-packaging.readthedocs.io/en/latest/minimal.html How To Package Your Python Code] - Python Packaging Tutorial</ref> <syntaxhighlight lang="python"> from setuptools import setup setup(name='namespace-my-package', version='0.1', description='My package description', url='http://github.com/dbarchowsky/namespace-my-package', author='Damien Barchowsky', author_email='dbarchowsky@gmail.com', license='MIT', packages=['namespace.my_package'], install_requires=['list', 'of', 'dependencies', ], zip_safe=False) </syntaxhighlight> After which the package can be installed locally with <syntaxhighlight lang="bash"> $ pip install . </syntaxhighlight> And once installed the package can be used like this: <syntaxhighlight lang="python"> >>> from namespace.my_package import my_module >>> my_module.some_routine() </syntaxhighlight> === Namespaces === It's recommended to use namespaces when creating packages. <ref>[https://packaging.python.org/guides/packaging-namespace-packages/ Packaging Namespace Packages] - PyPA Packaging Guide</ref> Even if the package is not being publicly distributed, the names of dependencies from other packages can clash. The recommended file structure is this: <pre> mynamespace-subpackage-a/ setup.py mynamespace/ subpackage_a/ __init__.py module_aa.py </pre> Note that there is no `__init__.py` in the `mynamespace` directory. Only packages should have `__init__.py` files. === Including sub-packages in distribution === Each module in the package must be explicitly added to the distribution under the `packages` setting in `setup.py`. For a project with packages under the top-level package, e.g.: <pre> mynamespace-subpackage-a/ setup.py mynamespace/ my_package/ __init__.py subpackage_a/ __init__.py subpackage_b/ __init__.py module_aa.py </pre> To include the non-top-level packages, add them to `packages` in `setup.py`: <syntaxhighlight lang="python"> from setuptools import setup setup(name='mynamespace-my-package', ''' [...] ''' packages=['mynamespace.my_package', 'mynamespace.my_package.subpackage_a', 'mynamespace.my_package.subpackage_b', ], ''' [...] ''' </syntaxhighlight>
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information