vendredi 11 août 2017

Avoid to import the path when using subfolders in python

Previously I was working without unittests and I had this structure for my project:

-main.py
   -folderFunctions:
       -functionA.py

Just using init file in folderFunctions, and importing

 from folderFunctions import functionA

everything was working good.

Now I have also unittests wihch I organized in this way:

-main.py
-folderFunctions:
    -functionA.py
    -folderTest:
       -testFunctionA.py

So I had to add (in order to run testFunctionA.py) in both functionA.py and testFunctionA.py these 2 lines to import the path:

 myPath = os.path.dirname(os.path.abspath(__file__))
 sys.path.insert(0, myPath + '../..') 

In this way the test works properly. But it is ugly to me and I guess also not very pythonic. Is there a way to make it more elegant?

Aucun commentaire:

Enregistrer un commentaire