Saturday, 2 July 2016

python relative import




I'm trying to make a relative import in python. but I can't understand the syntax and everytime I search for it here in SO, I can't get an answer:



Here is my folder structure:



Root
libraries
mylibrary
__init__

projects
project
myproject.py


and I want to import 'mylibrary' using a relative path, what's the syntax for doing it?


Answer



You have to add the directory to your python path.



import sys

sys.path.append("/libraries")


But I think it might be better to either put the libraries in the folders of the projects that need it or just install them to one of the standard places already in sys.path.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...