I want to develop a python application which is based on modularity. It is my first python application, so I try to sum up my situation as detailled as possible:
I am coding in Python for a few months. Now i am currently developing an application for statistics issues. Since I dont wan't to write my code into a single, endless .py-file , I intend to organize my application in single py files and related folders. So each py file is then related to a functionality which can be used by another .py file.
To give an example for my application folder structure:
My Directory structure is:
Notebook.ipynb
__init__.py
A/
function1.py
function2.py
function3.py
__init__.py
B/
function4.py
function5.py
function6.py
__init__.py
C/
imp.csv
The application shall be executed from the notebook.ipynb and uses the functions. E.g:
Notebook.ipynb
(uses) --> function1.py
(uses) --> function4.py
(imports) --> imp.csv
I have searched for ways here to realize that:
I found out that i could import modules (.py files) into other .py files.
Regarding to my example: function1.py would import function4.py from the sibling folder B. Due to the fact that the folder A and B are silbings, functionalities determining relative paths of a certain script would help me. But I got issues on using the most consulted ways of realizing that. I used:
__file__
throws the error "name file is not defined"
os.path.abspath("..") //
returns "C:/USERS" which is definetly not the parentfolder of the script I executed (real path is much deeper than that; something like C:USERS/name/folder1/folder2/applicationfolder/folderA)
Remark: I didn't run the whole script but (for testing purposes) the single statements in Spyder3 (Python3.6). My assumption here is that the issues are related to the execution of single statements.
---
My questions are:
- Am I correctly with my assumption?
- If yes: How do I test the import of a .py file?
- If no: what is the problem here?
- Regarding the final application in the end. I need a statement that ensures that the relative path is given for the script where it is is included and not the script which imports it and only uses its containing functions. So regarding to my example again:
When Notebook.ipynb(uses) --> function1.py
function1.py (uses) --> function4.py
function4.py (imports) --> 1.csv
I want to make sure that although notebook is the callee of the whole application, the statements in function1.py and function4.py are not returning the folder of Notebook.ipynb at runtime.
Because in the end there would be issues to load the csv file from another folder than function4.py.
MANY MANY THANKS IN ADVANCE!! :))
PS: I am also open minded for other suggestions that would help and work to structure my application source code