0

This is my project structure (simplified):

my-project
   ├── __init__.py
   ├── my_code.py
   └── tests
       ├── __init__.py
       └── test_foo.py

When I run pytest ( with pytest . from within my-project or pytest my-project from its parent directory), python assumes the tests directory as the root directory. However, I need python to identify my-project as the root directory. How do I do that?

Background:

test_foo.py uses a relative import (from ..my_code import *). If tests directory is the root directory, python will not be able to resolve the import. For structural reasons(1) I cannot use absolute paths. The relative import spawns the error ImportError: attempted relative import beyond top-level package

(1) my-project is maintained independently but is part (as a subfolder) in other projects. If the paths are absolute they would not be resolvable in the other projects. I use this structure over packaging, because packaging involves some management, that I need to avoid.

DarkTrick
  • 2,447
  • 1
  • 21
  • 39
  • Fundamentally, the module doing the relative import _must_ be part of a package, please refer to [this thread](https://stackoverflow.com/questions/16981921/relative-imports-in-python-3) – metatoaster Apr 19 '23 at 04:14
  • @metatoaster So you're saying that `my_project` is not a package? – DarkTrick Apr 19 '23 at 04:29
  • Based on the update, you may wish to consult [this thread about import beyond top level package](https://stackoverflow.com/questions/30669474/beyond-top-level-package-error-in-relative-import) instead (or [this one](https://stackoverflow.com/questions/49981741/)). As for your question, I cannot make any assertion where that is or is not a package but initially it was likely that it wasn't in a package, but really, use proper packaging techniques will avoid issues with imports not working, it will outweigh the cost of proper packaging of Python projects. – metatoaster Apr 19 '23 at 04:33
  • For what it's worth, based on your description, I could not recreate this error unless I removed `my_project/__init__.py`. Are you sure that file is present? – mipadi Apr 19 '23 at 04:36
  • You might want to post the entire file tree in which you are running these tests. – mipadi Apr 19 '23 at 04:38
  • @mipadi I rechecked, I have boiled the code down to (almost) exactly the structure stated in the question. See my answer below and the updated file structure in the question. Thank you very much for going through the hassle and trying my structure out! That was the keypoint to realize which parts were different. – DarkTrick Apr 19 '23 at 06:30

1 Answers1

1

Problem

The problem is the hyphen in the root folder ( my-project ).

If the folder contains a hyphen, python will ignore it as a root folder and take another folder as root folder (I don't know any details here, just that this happens).

Solution

You need to keep you code inside a python-acceptable folder. E.g. my_project.

DarkTrick
  • 2,447
  • 1
  • 21
  • 39