You're trying to make a module.
Start by installing the setuptools
package; on either Windows or Linux you should be able to type pip install setuptools
at a terminal to get that installed. You should now be able to write import setuptools
at a python prompt without getting an error.
Once that's working, set up a directory structure containing a setup.py
and a folder for your project's code to go in. The directory must contain a file called __init__.py
, which allows you to import
the directory as though it's a file.
some_folder/
| setup.py
| my_project/__init__.py
In setup.py
, drop the following content:
# setup.py
from setuptools import setup
setup(name="My Awesome Project",
version="0.0",
packages=["my_project"])
In my_project/__init__.py
, drop some stuff that you'd like to be able to import. Let's say...
# my_project/__init__.py
greeting = "Hello world!"
Now, in order to install the project at a system-wide level, run python setup.py install
. Note that you'll need to run this as root if you're on Linux, since you're making changes to the system-wide python libraries.
After this, you should be able to run python from any directory you like and type:
>>> from my_project import greeting
>>> print greeting
Hello world!
>>>
Note that this is enough to tell you how to make a module, but there's one hell of a lot of stuff that setuptools
can do for you. Take a look at https://pythonhosted.org/setuptools/setuptools.html for more info on building stuff, and https://docs.python.org/2/tutorial/modules.html for more info on how modules actually work. If you'd like to look at a package that (I hope) is reasonably simple, then I made my LazyLog module a couple of weeks ago on a train, and you're welcome to use it for reference.