Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python relative import [duplicate]

Tags:

python

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?

like image 629
Evandro Silva Avatar asked Jan 16 '23 12:01

Evandro Silva


1 Answers

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.

like image 143
camelNeck Avatar answered Jan 29 '23 12:01

camelNeck