How to import your own modules in Python without their path

You can import a module buil in in python or the modules you installed. Another way is to have the file to import in the same folder of the file that wants to import it. If you have scripts that you want to use often it could be useful to be able to import them even if they are not in the folder and you don’t know where they are.

This is useful if you want to manage a folder where you put all your little scripts in python that you want to use often importing them just like any other built in module or installed module, but without having to install anything. Just put your scripts in a folder and then let python know where it has to find them. This is good for little scripts.

Once you created a script in python that you put in a folder (ex.: mymodules), to let the computer know where your script it you have to put a .pth file in the folder where is python.exe.

To find this path, do what you see here. In the cmd print python or py or python3 and once python is running in the cmd import sys and then digit sys.path[4]

Here you can put the file.pth with the path to your personal modules

Now import os and do os.startfile(sys.path[4])

A window where you must put the .pth file will appear.

Now create a file where you write:

The file.pth file with the path to the folder with your own modules

Save this file as file.pth or any name.

Now you can import your own script even if the script is not in the folder you are working in.

You’re done. Now, if you want to convert some wav files in a folder, go in that folder open the cmd from that folder (write cmd in the address bar up in the window) and write py -m wav2mp3 or python -m wav2mp3 (you need to have a file that you called wav2mp3.py in the folder with your scripts with the code to convert the files, of course). The script will work into that folder.

Another interesting thing you can do is to create, like I did, a file called utility in which you create a tkinter window with a button that makes you start the script so that you do not need to remember how the script is called.

Here is an example. You could do it for all the scripts you use.

#timer.py
import tkinter as tk
import os


def ppr():
    print("click")


root = tk.Tk()
root.geometry("400x400")
root.title("Utility")
# root.geometry("400x400")


def convert():
	os.system("py -m convert_wav_mp3")


button = tk.Button(root, text="Converti wav in mp3", command=convert)
button.grid(row=2, column=0, sticky="w")



root.mainloop()

This video will explain you everything

Another use case for this

Another way you can use this is to create another folder where you put your batch files that calls a python script (or runs a module with py -m myscript). Creating a batch file, you can launch the script from the cmd just with digiting the name, like for any windows app that runs in the cmd. To do this you have to give the path of the folder with the batch files in the environmental variables.

H:\myapp_launcher is the path of the folder with the bath files, your can be different

To create a batch file, just write something like

and save it as rec.py

Writing rec in the cmd the rec.py script in the mymodules folder will run

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

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.