How to write a Python package/module?

python, python-2.7, pythonpath, windows

Solution

Well, a solution is to create a directory to add your modules and add this directory to the Python pat. For example, you can create a directory at, let us say, `C:\mypymodules` and put a `cls.py` file there with your function.

Now, let us add this directory to the Python path. Follow these instructions, just inserting `C:\mypymodules` in place of the directories mentioned there. Now, open a new command line window and try to import the module.

Another solution is to use distutils*. Instead of creating your own modules directory, in the same directory of your `cls.py` file create a file named `setup.py` with the following content:

from distutils.core import setup
setup(name='cls', py_modules=['cls'])

Then, just execute it:

> python setup.py install

This may install your module in the default Python library directories. Actually, this is the way (or, better yet, one of the ways) to pack, manage or python packages.

(I am not really using Windows, so some details may be broken. Nonetheless, I believe the general concepts are the same.)

* Some may argue about using setuptools but I think it is really overkill for your case. Anyway, if you want to learn more, see this question.

Problem

As I'm a novice, I started learning python by writing simple programs using python GUI .. everytime, when I try to clear the console I need to define the following few lines of code ``` import os def cls(): os.system('cls') print("Console Cleared") ``` and later I call it as ``` cls() ``` now I'm tired of writing the above code again and again(every time I open the gui window ).. so, I want to create a module called `cls` for future use to save my time .. in the end I'll call it as follows ``` import cls cls() ``` is there a way to do this ... Thanks !

Original source

Related problems