0

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

rilla119
  • 9
  • 1
  • 4
  • 1
    `abspath('..')` doesn't give you the path relative to the *location of the script*, it gives you the path relative to the current working directory of the process that runs the script. These are two different things. – Charles Duffy Dec 13 '17 at 21:01
  • With respect to `__file__` not working -- how exactly are you testing? Can you generate a [mcve] others can use to see the problem themselves? (Providing a MCVE that reproduces a specific, distinct problem in the shortest possible code is also a defense against being closed as "too broad"). – Charles Duffy Dec 13 '17 at 21:03
  • It works perfectly for me, f/e, when I run `echo 'import os.path; print(os.path.abspath(__file__))' >test.py; python3.6 test.py` -- do you get an error with that same code? Just as I provided a one-line copy-and-pasteable example that shows (hopefully) that and how it works, can you provide a very short copy-and-pasteable example that shows how it breaks in your example or use case? – Charles Duffy Dec 13 '17 at 21:05
  • ...if you only have problems with `__file__` in Spyder, then that's an IDE issue, and I'd suggest asking a question scoped specifically to the IDE and how you're invoking your code within it. – Charles Duffy Dec 13 '17 at 21:12
  • @CharlesDuffy Thanks! It is a Spyder "issue" and my assumption is right. When I run my script __file__ is working! – rilla119 Dec 13 '17 at 21:31

0 Answers0