Skip to content

Instantly share code, notes, and snippets.

@TheProjectsGuy
Created April 19, 2022 04:49
Show Gist options
  • Save TheProjectsGuy/a998c0a53313ee0bdc937018603d93a4 to your computer and use it in GitHub Desktop.
Save TheProjectsGuy/a998c0a53313ee0bdc937018603d93a4 to your computer and use it in GitHub Desktop.
A gimmick to add a path to python interpreter
import os
import sys
from pathlib import Path
# Set the "./../lib" from the script folder
dir_name = None
try:
dir_name = os.path.dirname(os.path.realpath(__file__))
except NameError:
print("WARN: __file__ not found, trying local")
dir_name = os.path.abspath('')
lib_path = os.path.realpath(f"{Path(dir_name).parent}/lib")
# Add to path
if lib_path not in sys.path:
print(f"Adding library path: {lib_path} to PYTHONPATH")
sys.path.append(lib_path)
else:
print(f"Library path {lib_path} already in PYTHONPATH")
@TheProjectsGuy
Copy link
Author

Allows you to add a library folder to PYTHONPATH. This is done if you want to use a library and are too lazy to create a python package out of that library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment