Skip to content Skip to sidebar Skip to footer

What Is The Best Practice For Importing Modules: Absolute Paths Or Relative Paths?

So I was working on a project where I defined all paths to other Python modules as absolute paths: import sys sys.path.append('/home/user123/my_project/utilities') import file_util

Solution 1:

A handy solution would be to "move" a basepath, i.e. basepath = "/home/user123/" into a variable and address the path as

sys.path.append('{}/my_project/utilities'.format(basepath))

That would ugly relative notation and give you flexibility.

Post a Comment for "What Is The Best Practice For Importing Modules: Absolute Paths Or Relative Paths?"