Use your own module on python

Let’s create a calculator to see how to use your own modules with python, the way you can use them in any place, not just having them in the work directory.

cmd

You can use this way to get the result from a module of your.

The code of this module is really simple.

import sys

print(eval(sys.argv[1]))

What does he do? It takes the argument (in the example is 4*5/8*(9+3) and gives you the result (with eval). The result is this, by the way:

So, you have your little calculator, without having to start python.

If you have the calc.py in the directory where the cmd is, there are no problems, otherwise you have to:

go check where the site-packages dir in the python installation is and put a file in there. To find where is this dir, you can do:


Then you follow the path (in this case is C:\\Users\\giova\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages).

Now save a file like mymodules.pth (any name is good, the file extension is the important thing), and write the path of the dir where you want that Python searches for the modules. You’re done. Now you can launch your modules like any built in module.

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.