0

I am having a issue importing a python file from another directory. Below is my project layout.

project/
   include/
     networking/
       ssl.py
     process.py

Inside my ssl.py file I am trying to access a function inside process.py

I have tried

from include.process import procfunction

This returns a error

cannot import name 'procfunction' from 'include.process'
Doritos
  • 403
  • 3
  • 16
  • 1
    Possible duplicate of [Importing modules from parent folder](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) – Alessi 42 May 31 '19 at 16:36
  • 1
    I think I had similar problem before. I have learned that it has to do with where you execute your program. If your working directory is at parent level for example project, and you execute ssl.py, it should be ok. Maybe [this](https://stackoverflow.com/questions/20075884/python-import-module-from-another-directory-at-the-same-level-in-project-hierar) post can help too – Xp.L May 31 '19 at 16:41

2 Answers2

1

When you run a python file and have import statements, it is looking at your current directory of that file. In order to move out 1 directory use '..'

Example - Inside your ssl.py:

from ..process import procfunction
Rion
  • 31
  • 5
  • Just to add to this, am i correct in saying that each directory needs to contain a __init__.py file so that it is registered as a module, or will this method work irrespective of an init files presence ? – Brandon Bailey May 31 '19 at 16:58
  • I was actually looking into this yesterday [Main Difference Between a Python Module and Package](https://stackoverflow.com/questions/7948494/whats-the-difference-between-a-python-module-and-a-python-package). To my understanding, the main difference between a module and package is that a module is 1 file, and a package is multiple files in a directory, with a __init__.py – Rion May 31 '19 at 17:00
-1

You may need to move back up into your include directory. If you're on a Linux this can be done via

import os
os.system("cd ..")
from process import procfunction 

My apologies if this does not help.