0

I wanted to import a file from another folder into another file in the same root folder.

Directory list:

ROOT/
    resource/
        console_log/
            __init__.py (empty)
            file1.py
        libs/
            __init.py__ (empty)
            function.py
    __init__.py (empty)

I tried using this code in resource/console_log/file1.py:

from resource.libs.function import function_name

It just doesn't work. Returns this error:

No module named 'resource'

If I use this code instead

from ..libs.function import function_name

It gives me this:

attempted relative import with no known parent package

How can I solve?

Is there any way to fix it without using the sys?

EDIT:

I "fixed" the problem by moving all the files directly to the libs folder thus removing the resource folder. Except that if from libs/file1.py I want to import a function present in the main.py file I get the same error

New folder structure:

ROOT/
    libs/
        __init__.py
        file1.py
        file2.py
        function.py
    logs/
        debug.log
    __init__.py
    main.py

If I use this code in the libs/file1.py file, it works correctly (only if I start it from the main.py file)

# file1.py
from libs.function import function_name

But if in libs/file2.py I want to recall a variable present in the main.py file, it returns me an error

# file2.py
from main import data

# ERROR
No module named 'main'

If he doesn't give me this error he gives me another one attempted relative import with no known parent package

Matteo
  • 118
  • 2
  • 12
  • This asnwer could help you problem https://stackoverflow.com/a/11393586/14466874 – Unspoiled9710 Jun 07 '21 at 19:43
  • @martineau Dupe of https://stackoverflow.com/a/43476575/674039 Why reopened? – wim Jun 07 '21 at 19:53
  • @wim: Because I don't think your answer to the other question is platform-independent — and even if you think it now is, it's not clear that something that changes an environment variable temporarily is a solution to this problem (because scripts are not always executed through a shell). – martineau Jun 07 '21 at 20:04
  • Then you didn't read the answer fully, or you've glossed over the part which said an installer (setup.py or pyproject.toml) is needed in order to place the code into site-packages. – wim Jun 07 '21 at 20:05
  • 1
    ..... aaaaand now the duplicate PYTHONPATH answers are coming in. :) – wim Jun 07 '21 at 20:06
  • @wim: There's no installers involved _here_. And the fact that others are making the same assumptions as you proves nothing. – martineau Jun 07 '21 at 20:06
  • How do you use `resource/console_log/file1.py`? Do you run it as a script? If so, what is the working directory? Or do you import it as a module in another file? – mkrieger1 Jun 07 '21 at 20:17
  • 1
    Well the code needs to get on `sys.path` somehow, this is a pretty clear dupe of [ImportError: No module named ](https://stackoverflow.com/a/43476575/674039) isn't it? The other issue with "attempted relative import with no known parent package" is what you get when you attempt to run a submodule within a package as a script, that one is well covered over on [Relative imports for the billionth time](https://stackoverflow.com/q/14132789/674039). – wim Jun 07 '21 at 20:18
  • @mkrieger1 I run the script directly from visual studio code – Matteo Jun 08 '21 at 14:08

1 Answers1

0

I think your PYTHONPATH isn't set correctly. You can either export it or run your program with it set as expected for the single command. The following will set the PYTHONPATH for the single command execution:

PYTHONPATH=./:$PYTHONPATH python resource/console_log/file1.py
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Rachel
  • 336
  • 1
  • 5
  • I honestly think it is set correctly, also because if I go to the `ROOT/main.py` file I can import the file in the `libs/function.py` – Matteo Jun 08 '21 at 09:33