How do I write a .pth file?

I want to change the directories in which Python looks for my packages and modules, and changing PYTHONPATH in Windows 10 Environment Variables does not work. So the only solution left (apparently) is to place a .pth file in my package folder.

How do I create a .pth file ? How can I write in it, how can I open it ?

Also, what should I write in it in order for Python to find it and to be able to import my package ?

1 Answer

The question is old I guess, adding this answer if someone lands here! For windows 10 users, execute the following code using python. Every time you change the path_to_add variable and execute this code, a new path will be appended to the custom_path.pth file.

# site_packages_path is the packages folder, which in my case is:
site_packages_path = r'C:\Users\Dhwani\AppData\Local\Continuum\anaconda3\Lib\site-
packages'
# path that you wanna add, which again in my case is
path_to_add = "C:\Users\P077172\Documents\Jupyter"
f = open(site_packages_path + "\custom_path.pth", "a")
f.write(path_to_add)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like