How can I create a simple system wide python library? -
i see there built in packages can import script like:
from datetime import date today = date.today() print today
how can create simple package , add system library can import datetime
in above example?
you're trying make module.
start installing setuptools
package; on either windows or linux should able type pip install setuptools
@ terminal installed. should able write import setuptools
@ python prompt without getting error.
once that's working, set directory structure containing setup.py
, folder project's code go in. directory must contain file called __init__.py
, allows import
directory though it's file.
some_folder/ | setup.py | my_project/__init__.py
in setup.py
, drop following content:
# setup.py setuptools import setup setup(name="my awesome project", version="0.0", packages=["my_project"])
in my_project/__init__.py
, drop stuff you'd able import. let's say...
# my_project/__init__.py greeting = "hello world!"
now, in order install project @ system-wide level, run python setup.py install
. note you'll need run root if you're on linux, since you're making changes system-wide python libraries.
after this, should able run python directory , type:
>>> my_project import greeting >>> print greeting hello world! >>>
note enough tell how make module, there's 1 hell of lot of stuff setuptools
can you. take @ https://pythonhosted.org/setuptools/setuptools.html more info on building stuff, , https://docs.python.org/2/tutorial/modules.html more info on how modules work. if you'd @ package (i hope) reasonably simple, made lazylog module couple of weeks ago on train, , you're welcome use reference.
Comments
Post a Comment