0

I have a folder called Python Modules which contains single-file modules I've made that I often use, for example one called mytimer.py. I added Python Modules to my Windows PYTHONPATH environment variable and I import it in other files with import mytimer.

But now I would like to put mytimer in a git repo, so I would need to put it in a folder (I don't want all my modules in a single repo) like Python Modules\mytimer\mytimer.py.

And I want to do this for many of the single-file modules in that folder.

Is there any way I can do this while still being able to import like import mytimer rather than import mytimer.mytimer, other than adding each folder individually to the PYTHONPATH?

martineau
  • 99,260
  • 22
  • 139
  • 249
Esostack
  • 2,388
  • 2
  • 20
  • 42

1 Answers1

1

Make the relevant python session aware of that directory, by adding that path to sys.path:

import sys
sys.path.append('/path/to/repo/folder')
import a_module_in_that_folder

If you want to permanently add this path to sys.path, that's been answered here

ShlomiF
  • 1,285
  • 8
  • 13
  • That works, but I was hoping there would be some way I could set it up (maybe by doing something in `__init__.py` or PYTHONPATH) that wouldn't require me modifying all of the files that are already using `import mytimer`. – Esostack Dec 03 '18 at 21:36
  • Updated to refer to a different post that should help – ShlomiF Dec 03 '18 at 21:39
  • Thanks! That does solve my problem, I added a .pth file to my site-packages folder and wrote to it all the folder names in my `Python Modules` folder. This isn't as elegant a solution as I was hoping for but I think it's better than spamming my Windows PYTHONPATH – Esostack Dec 03 '18 at 21:51