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 !
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:\mypymodulesand put acls.pyfile there with your function.Now, let us add this directory to the Python path. Follow these instructions, just inserting
C:\mypymodulesin 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.pyfile create a file namedsetup.pywith the following content:Then, just execute it:
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.