4

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 !

3 Answers3

3

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.

Community
  • 1
  • 1
brandizzi
  • 26,083
  • 8
  • 103
  • 158
2

If you put your cls() function in a file called cls.py, and put that either in the same directory as the program that's calling it or else somewhere in your PYTHONPATH on your system, then you can use your function like this:

import cls

cls.cls()

This is because the cls() function is inside the cls module (the name of the module is determined by the file name).

If you want to just be able to do cls() then do this:

from cls import cls

cls()
Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73
  • thanks, btw how can I access the same module when I'm in another directory .. for example, I can access `os` module from anywhere .. similarly i want to access it Globally –  Jun 19 '12 at 16:23
  • 1
    You need to put it in your PYTHONPATH, or add to your PYTHONPATH to include the directory it is in. This is unfortunately kind of complex and I do not think I can explain the concept fully in comments -- search StackOverflow for other questions about PYTHONPATH for more info. – Andrew Gorcester Jun 19 '12 at 16:33
1

You can spice it up a little bit by making your module cross platform. Here it is:

import os, sys
def cls():
    if 'win' in sys.paltform:
        os.system('cls')
        print 'Console cleared'
    else:
        os.system('clear')
        print 'Console cleared'

Now, you can use it in any other script using:

from cls import cls
cls()
Vishwanath
  • 835
  • 1
  • 7
  • 19
  • This does not answer the question. Actually, this not even is correct, since there is no parenthesis after the function name. – brandizzi Jun 19 '12 at 16:28
  • by the way `if 'win' in sys.platform` will trigger for mac and windows (`darwin` and `win32`). should just check for `win32` or use startswith at least – GP89 Jun 19 '12 at 16:28