1

Based on some answers I try to be more specific. I want to import the print and the models AND code in my main.py

I know the question gets asked a lot, but still I could not figure out whats wrong with my code! I have a project directory like this

-project
   --__init__py
   --main.py
   --print.py
   --requests
        --__init__.py
        --models.py
        --code.py

i want to import from print.py and * from requests Therefore I tried to add these lines in main.py

from . import print
#or
import print

#for requests I tried
import os.path
import sys
sys.path.append('./requests')
from requests import *

all of those lines cause the same ImportError attempted relative import with no known parent ,

using Python 39 anyone an idea where the problem is? I am very confused that this seems not to work, was it possible in older versions?

Maxi
  • 11
  • 2

2 Answers2

1

You should definitely not be doing anything with sys.path. If you are using a correct Python package structure, the import system should handle everything like this.

From the directory structure you described, project would be the name of your package. So when using your package in some external code you would do

import package

or to use a submodule/subpackage

import project.print
import project.requests

and so on.

For modules inside the package you can use relative imports. When you write

i want to import from print.py and * from requests Therefore I tried

it's not clear from where you want to import them, because this is important for relative imports.

For example, in project/main.py to import the print module you could use:

from . import print

But if it's from project/requests/code.py you would use

from .. import print

As an aside, "print" is probably not a good name for a module, since if you import the print module it will shadow the print() built-in function.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
  • The thing is, I want to use the modules within the project, so import them to the main.py. Note: print is not the real name! – Maxi Aug 18 '21 at 12:27
  • So in `main.py` you would use `from . import print` and so on. – Iguananaut Aug 18 '21 at 12:28
  • As you wrote how to use the relative imports, thats exactly what I wanted to do and is not working. – Maxi Aug 18 '21 at 12:30
  • I think you may need to provide more details as to exactly what you did. In what code did you write the import statements you showed? – Iguananaut Aug 18 '21 at 12:31
  • The error you're getting is one that would be occurring if you attempt a relative import from a module that is not in a package. – Iguananaut Aug 18 '21 at 12:31
  • I wrote the import statements in my main.py – Maxi Aug 18 '21 at 12:37
  • How are you using main.py? As written, you would not be able to write, e.g. `python project/main.py`. If that's what you're doing then [this answer](https://stackoverflow.com/a/68832223/982257) is correct. What you can do instead is create a `__main__.py` and run `python -m project` as explained [here](https://stackoverflow.com/a/36320295/982257) – Iguananaut Aug 18 '21 at 12:41
  • Basically, if you are running `python project/main.py` it does not get imported by the import system as part of the package, but rather as the special `__main__` module. This means package-relative imports won't work because as far as the import system is concerned the `__main__` module is not part of any package (except in the special case of `packagename/__main__.py`). Alternatively, in `main.py` you can use absolute imports instead of relative imports. – Iguananaut Aug 18 '21 at 12:43
0

Your main file should be outside the 'project'-directory to use that as a package. Then, from your main file, you can import using from project.print import ....

Within the project-package, relative imports are possible.