I have a file that is a library for API calls that I use across various projects. This means that I have to copy the file api.py to every project and import api
. When I need to make changes to this api.py file, I have to do it in multiple locations. Where can I store this so that I can access it globally from any of my local projects?
Asked
Active
Viewed 90 times
-2

R.F.
- 73
- 7
-
It depends on your operating system and Python version. On Linux, you can put it in `~/.local/lib/python3.X/site-packages` (substitute your version) without requiring special permissions. – Tim Roberts Aug 19 '21 at 01:56
-
1You probably either want to set PYTHONPATH (if it's just for you; see https://stackoverflow.com/questions/7850908/) or learn Python packaging and distribution to distribute your `api.py` as a package (see https://packaging.python.org/tutorials/packaging-projects/). I recommend packaging; it will be useful in the long run. – dwhswenson Aug 19 '21 at 02:02
1 Answers
1
Use the sys module:
import sys
sys.path.append(<your path here>)
import api

LuckElixir
- 306
- 3
- 14
-
This is really hacky and not ideal, and not the primary use case for this. people shouldn't generally be doing this, instead, use your operating system tools to configure the python path. – juanpa.arrivillaga Aug 19 '21 at 02:25