This question needs a proper answer:
Just use the standard package site
, which was made for this job!
and here is how (plagiating my own answer to my own question on the very same topic):
- Open a Python prompt and type
>>> import site
>>> site.USER_SITE
'C:\\Users\\ojdo\\AppData\\Roaming\\Python\\Python37\\site-packages'
...
- Create this folder if it does not exist yet:
...
>>> import os
>>> os.makedirs(site.USER_SITE)
...
- Create a file
sitecustomize.py
in this folder containing the content of FIND_MY_PACKAGES
, either manually or using something like the following code. Of course, you have to change C:\My_Projects
to the correct path to your custom import location.
...
>>> FIND_MY_PACKAGES = """
import site
site.addsitedir(r'C:\My_Projects')
"""
>>> filename = os.path.join(site.USER_SITE, 'sitecustomize.py')
>>> with open(filename, 'w') as outfile:
... print(FIND_MY_PACKAGES, file=outfile)
And the next time you start Python, C:\My_Projects
is present in your sys.path
, without having to touch system-wide settings. Bonus: the above steps work on Linux, too!
site
module. – Zeinab Abbasimazar