OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to reference another module in the folder parallel with the parent folder in Python? [duplicate]

  • Thread starter Thread starter hh54188
  • Start date Start date
H

hh54188

Guest
Here is my project folder structure

Code:
|-- root
    |-- my_modules
        |-- __init__.py
        |-- main.py
        |-- m.py
    |--pipelines
        |--p.py

Basically there is nothing in the __init__ and main files of my_module folder, I just give a simple public method in the m.py file:

Code:
def foo():
   print('foo() in m.py')

I want to import the foo method in the p.py in the pipelines folder, and here is my code:

Code:
import sys

sys.path.append('..')
from my_modules import m

def p():
   m.foo()
   print('p')

p()

My question is, after I append the parent folder in the search path with sys.path.append, why it still cannot find my_mofules? How can I fix this problem?
<p>Here is my project folder structure</p>
<pre><code>|-- root
|-- my_modules
|-- __init__.py
|-- main.py
|-- m.py
|--pipelines
|--p.py
</code></pre>
<p>Basically there is nothing in the <code>__init__</code> and <code>main</code> files of <code>my_module</code> folder, I just give a simple public method in the <code>m.py</code> file:</p>
<pre class="lang-py prettyprint-override"><code>def foo():
print('foo() in m.py')
</code></pre>
<p>I want to import the <code>foo</code> method in the <code>p.py</code> in the <code>pipelines</code> folder, and here is my code:</p>
<pre class="lang-py prettyprint-override"><code>import sys

sys.path.append('..')
from my_modules import m

def p():
m.foo()
print('p')

p()
</code></pre>
<p>My question is, after I append the parent folder in the search path with <code>sys.path.append</code>, why it still cannot find <code>my_mofules</code>? How can I fix this problem?</p>
 

Latest posts

Top