簡體   English   中英

如何創建我自己的 Python package 並將其保存在我的電腦上

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

我創建了一個 package。 這是一個 function 我需要在我的幾乎每個 Tkinter 項目中使用。 它將我的 Tkinter window 在用戶的屏幕上居中。 如何使它像“import pandas”或“import math”之類的全局 package?

這是我的 package 的代碼:

我的包

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))

這是我正在處理的 Tkinter 項目: 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()

一切都按預期工作:

我的行動計划圖片

但是我想這樣做,這樣我就不需要將這個MyPkg.py放在與我的 Tkinter 項目相同的文件夾中,因為我制作了許多不同的軟件,並且每個軟件都有自己的文件夾。

在此處輸入圖像描述

我希望能夠從任何目錄將其導入計算機上的任何位置。 接下來我可以嘗試什么?

在 unix 機器上,將您的模塊添加到 /usr/lib/python3.9/ 或 python 搜索模塊的其他路徑之一就可以了。 希望在 windows 上做等效的工作(路徑當然不會相同)。

注意:這類東西在 C 之類的語言中更常見,這些語言沒有 package 管理器,例如 pip。

Go 至

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

然后在系統變量下創建一個名為PYTHONPATH的新變量。 在此變量中添加模塊的位置。

蟒蛇路徑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM