0

I need some help. I have been stuck with this for a while. I cannot figure out how and what to do.

I have a directory "folder" with the below structure.

.
├── folder1
│   ├── folder3
│   │   ├── a.py
│   │   └── __init__.py
│   └── __init__.py
├── folder2
│   ├── b.py
│   └── __init__.py
└── __init__.py

What I want to basically do is import a function in the "folder2/b.py" python file into "folder1/folder3/a.py". And I want to run the 'a.py' file from "folder1/folder3/" directory.

Let me illustrate this with an example. Suppose I have a function 'hello' in 'b.py'.

def hello():
    print("Hello world!")

And I have another file 'a.py' like this.

from b import hello

def hello1():
   print("Hello World!!")

I want to navigate to the 'folder3' directory.

cd folder1/folder3

And run the python file 'a.py' as follows.

python a.py

Doing so is giving me the below error.

ValueError: attempted relative import beyond the top-level package

I googled for a while and tried solving this with these resources (Import a module from a relative path, relative path not working even with init.py, How to fix “Attempted relative import in non-package” even with init.py).

The only method that worked so far is by using "sys.append" in 'a.py' like below. However, I have read that this usage is very unprofessional and highly unrecommended.

import sys
sys.path.append("D:/folder")

from folder2 import b

If you have dealt with such a setup/issue before, please help me out. Thanks!

Sudheer Raja
  • 151
  • 1
  • 8
  • 1
    The question is why does your setup look like this. Normally your project resides in the root directory and any other package your project requires is installed through pip or are present as children of your root folder. So there should be no reason to go one folder level up. You should turn it into a python package. Your project shouldn't reach a beyond the top level folder. – Tin Nguyen Jun 30 '20 at 08:22
  • Which OS? At least in Linux this is usually this is done by adding folder2 to the `PYTHONPATH` environment variable. – Jonatan Öström Jun 30 '20 at 08:24
  • @JonatanÖström Yes. But I don't want to use the **PYTHONPATH** approach. Any other ideas? – Sudheer Raja Jun 30 '20 at 08:25
  • @TinNguyen I don't agree, this seems normal to me. – Jonatan Öström Jun 30 '20 at 08:25
  • Then maybe see this question https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path on how to import from absolute path using importlib. – Jonatan Öström Jun 30 '20 at 08:27
  • @TinNguyen This structure allows me to quickly plug in new applications into the root folder reusing the libraries. Any idea on how to make it work this way? – Sudheer Raja Jun 30 '20 at 08:28
  • @napuzba Thanks for sharing the article. The post only describes accessing files one level up. Any idea how to go two levels up? – Sudheer Raja Jul 01 '20 at 04:44
  • @SudheerRaja added a answer with more details. – napuzba Jul 01 '20 at 07:46
  • Can you please [edit] your question to show *how* you are trying to navigate from ``a`` to ``b``? ``a`` doing ``from b import hello`` cannot cause the error shown. Also, which file do you execute, and how? – MisterMiyagi Jul 01 '20 at 07:59
  • Does this answer your question? [Attempted relative import beyond toplevel package](https://stackoverflow.com/questions/40022220/attempted-relative-import-beyond-toplevel-package) – MisterMiyagi Jul 01 '20 at 08:02
  • Does this answer your question? [Import a module from both within same package and from outside the package in Python 3](https://stackoverflow.com/questions/47319423/import-a-module-from-both-within-same-package-and-from-outside-the-package-in-py) – MisterMiyagi Jul 01 '20 at 08:03
  • Does this answer your question? [Correct import and package structure now that __init__.py is optional](https://stackoverflow.com/questions/51397669/correct-import-and-package-structure-now-that-init-py-is-optional) – MisterMiyagi Jul 01 '20 at 08:04

1 Answers1

-1

Try adding your root folder to your python path.

Be Chiller Too
  • 2,502
  • 2
  • 16
  • 42
  • That's what I am doing right now. But I read in some posts on StackOverflow that it's not professional to do that way. So any other ideas? – Sudheer Raja Jun 30 '20 at 08:30
  • Yes indeed, don't modify the python path within your code, but modify your environment variable outside of your code: https://duckduckgo.com/?t=ffab&q=modify+python+path&ia=web – Be Chiller Too Jun 30 '20 at 08:31