Search code examples
pythonpython-3.xtkinterpackagepython-packaging

How to create my own Python package and save it on my computer


I created a package. It's a function I need to use in almost every Tkinter project of mine. It centers my Tkinter window on the screen of the user. How do I make it like a global package like "import pandas" or "import math"?

This is the code for my package:

MyPkg.py

def Centre(NameOfTkinterWindow,Width,Height):#This function Centres the Tkinter window on the screen
    scrwdth = NameOfTkinterWindow.winfo_screenwidth()
    scrhgt = NameOfTkinterWindow.winfo_screenheight()
    xLeft = int((scrwdth/2) - (Width/2))
    yTop = int((scrhgt/2) - (Height/2))
    NameOfTkinterWindow.geometry(str(Width) + "x" + str(Height) + "+" + str(xLeft) + "+" + str(yTop))

This is my Tkinter project that I am working on: main.py

from tkinter import *
from MyPkg import * #importing my custom built package
main=Tk()
main.title("Test Taking App")
Centre(main,500,500) #A function from my Package
main.iconbitmap("D:\Coding\Python Scripts\PDF Convertor App\DevenIcon.ico")
main.mainloop()

Everything is working as intended:

Image of my Program in Action

But I want to make it so that I don't need to have this MyPkg.py in the same folder as my Tkinter projects as I make many different softwares and each one has their own folder.

enter image description here

I want to be able to import it anywhere on my computer from any directory. What can I try next?


Solution

  • Go to

    My Computer > Properties > Advanced System Settings > Environment Variables >
    

    Then under system variables create a new Variable called PYTHONPATH. In this variable add the location of your modules.

    PYTHONPATH