1

I know there are probably thousands of posts like this and i nearly read them all and still i could not fix my issue.

I have the following project structure:

project
│
├── main.py           
├── module1
│   ├── __init__.py    
│   ├── b.py    
│   └── c.py
│
├── tests   
│   └── tests_module1.py          
                       

in tests_module1.py i want to import functions b.py and c.py to test their functionality. I already tried the following imports:

  • from ..module1.b import *
  • from module1.b import *
  • from .. import b

I also tried adding init.py files to the project as well as the test folder with no succsess. It only works when i move the test_module1.py into the project folder and import with :

  • from module1.b import *

I am using Python 3.9

Florian
  • 95
  • 1
  • 8

3 Answers3

1

See this post. Python does not know the package structure if you run python3 tests_module1.py from within the tests directory.

Try the following b.py:

# module1/b.py

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

Import it in tests_module1.py:

from module1 import b
b.hello()

Run it from the project directory with the -m (run as module) switch:

python3 -m tests.tests_module1
maij
  • 4,094
  • 2
  • 12
  • 28
  • Thank you, running it from the project directory was the right hint. It is quiet obvious now that when I startet the test_module1.py inside the Test directory That the Interpreter doesn’t know anything about the module1. – Florian Mar 10 '21 at 15:05
0

instead of from .. import b you must try

from ...module1 import *
Rajat Soni
  • 151
  • 12
  • This raises: ImportError: attempted relative import with no known parent package – Florian Mar 09 '21 at 10:37
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/28492441) – Lee_Dailey Mar 09 '21 at 15:36
  • @Lee_Dailey: Why do you say that? This may not be a _correct_ answer, but it’s certainly _an_ answer. – Jeremy Caney Mar 10 '21 at 05:20
  • @JeremyCaney - to me, the `try` means it is a suggestion that really otta be made in a _comment_. – Lee_Dailey Mar 10 '21 at 14:04
  • @Lee_Dailey: It’s okay to suggest that the OP “try” something ([source](https://meta.stackoverflow.com/a/256360/3025856)]. Such answers might merit downvoting, but not deletion. Same with code-only answers. Neither are considered _quality_ answers, but that’s what the downvote option is for. The _VLQ_ queue is for posts that make no effort to answer the question (i.e., “thanks”, “this didn’t work”, “I’m having this problem too”, or just a link) ([source](https://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue)). – Jeremy Caney Mar 10 '21 at 15:40
  • @JeremyCaney - ah! thank you for the clarification. [*grin*] – Lee_Dailey Mar 10 '21 at 16:47
0

Have a look at modifying the python search path: https://docs.python.org/3.9/install/index.html#modifying-python-s-search-path

Modifying Python’s Search Path

When the Python interpreter executes an import statement, it searches for both Python code and extension modules along a search path. A default value for the path is configured into the Python binary when the interpreter is built. You can determine the path by importing the sys module and printing the value of sys.path...

The expected convention for locally installed packages is to put them in the …/site-packages/ directory, but you may want to install Python modules into some arbitrary directory... There are several different ways to add the directory.

The most convenient way is to add a path configuration file to a directory that’s already on Python’s path, usually to the .../site-packages/ directory. Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path...

<<snip several other options>>

Finally, sys.path is just a regular Python list, so any Python application can modify it by adding or removing entries.

See also

dbc
  • 104,963
  • 20
  • 228
  • 340
rauberdaniel
  • 1,017
  • 9
  • 22
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/28494010) – codingCat Mar 09 '21 at 14:11
  • @codingCat Understood, thank you for the hint – rauberdaniel Mar 09 '21 at 14:45