0

Introduction

I'm having problems with importing my own modules/packages so I searched up some tutorials (written and youtube) and was trying to solve my problems. However nothing really worked so I recreated the structure of a tutorial to follow it step by step (sweetcode.io).

I'm using Python 3.9.13 installed via anaconda on a win10 machine

Structure of the sample project

-- tutorial
   |-- subfolder1
   |    |-- subfolder2
   |    |   |-- __init__.py
   |    |   |-- student.py
   |    |   `-- user.py
   |    |-- __init__.py
   |    `-- item.py
   |-- subfolder3
   |   |-- __init__.py
   |   |-- accounts.py
   |   `-- registration.py
   |-- __init__.py
   `-- security.py

I could follow the tutorial up to the part where they talk about importing a module backwards, so say from student.py import the module security.py. However as soon as I try to do that I get an error message.

Attempt 1:

code in student.py:

import tutorial.security

powershell command (currently inside the tutorial directory):

python .\subfolder1\subfolder2\student.py

error received:

Traceback (most recent call last):
  File "C:\Users\Schnetzubroot\Documents\test\subfolder1\subfolder2\student.py", line 1, in <module>
    import tutorial.security
ModuleNotFoundError: No module named 'tutorial.security'

Attempt 2:

if I change the code in student.py to: from test import security I still get the same error message

Attempt 3:

When trying to use a relative import as such: from ... import security i get the following error message:

Traceback (most recent call last):
  File "C:\Users\Schnetzubroot\Documents\tutorial\subfolder1\subfolder2\student.py", line 1, in <module>
    from ... import security
ImportError: attempted relative import with no known parent package

If somebody could tell me what I'm doing wrong I'd really appreciate it because I don't see what I'm doing wrong. Also any feedback to the post is appreciated as this is my first question here

Orange
  • 1
  • 1
  • Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Brian61354270 Feb 18 '23 at 19:12
  • I'm afraid that's a very good tutorial. I find it pretty misleading about how directories relate to packages, and about how Python's import system actually works. In particular, imports _do not navigate directories_ (they are only resolved by searching the Python path), and it's incorrect about the significance of `__init__.py` files. See the linked question and the Python [import system documentation](https://docs.python.org/3/reference/import.html) for better coverage. – Brian61354270 Feb 18 '23 at 19:18
  • @Brian I've read the first post which you gave me ([relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time)) and think I understand the mentioned difference between scripts vs. modules, but I can't figure out how that affects my example, since I just want to import the module in a "backwards kind of way" as I would if it was just in the same directory or a subdirectory – Orange Feb 18 '23 at 22:27

3 Answers3

2

You should not use "import tutorial.security". Instead use "import security", or just to be sure use this.

lotus
  • 116
  • 6
  • This fixes the problem for the moment. However I'm trying to achieve this without the need for the sys import, as the code gets developed on different machines and thus different paths. Is there maybe an elegant way to make this work with different machines? – Orange Feb 18 '23 at 22:31
1

It looks like that page is missing some key details for use as a true tutorial.

Attempt 1 & 2

The issue here seems to be that the root "tutorial" directory is not on your python path for anaconda. Instructions for doing this using Anaconda can be found here.

Attempt 3

The issue here is you are trying to run the python file as a script. (i.e. __name__ == "__main__". See the Python Docs (6.4.2. Intra-package References) note on this. It appears the intent for relative imports is not to be used in an Python script, only for internal uses.

0

I believe the correct way of doing the 'from' import would be:

from tutorial import security

Otherwise, you can also add the parent folder 'tutorial' to your system path and then just import security directly, like so:

import security
import sys

sys.path.insert(0, path/to/tutorial/folder)
  • the `from tutorial import security` doesn't work and still throws an error (no module named 'tutorial'). – Orange Feb 18 '23 at 22:34
  • Doing it the sys.path way does work, however is not my preferred solution as I don't want to adjust the path every time I switch machines when developing the code. Maybe there is an elegant way to solve this problem however – Orange Feb 18 '23 at 22:35