2

I've read a few Python relative vs. absolute import tutorials and can't figure out this ModuleNotFound error for the life of me.

I'm working with the following directory structure:

project
 |    
 +-- pseudo
 |  |  
 |  +-- __main__.py  
 |  |
 |  +-- pseudo.py
 |  |  
 |  +-- analytics_generator
 |      |
 |      +-- analytics_generator.py
 |      |
 |      +-- models
 |         |
 |         +-- blueprint.py 

The root of the problem is that in the analytics_generator.py file, I'm trying to import SomeClass from blueprint.py.

When I execute the main function in __main__.py, I get the following error:

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1741, in <module>
    main()
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File ".../project/pseudo/__main__.py", line 2, in <module>
    from pseudo import Pseudo
  File ".../project/pseudo/pseudo.py", line 4, in <module>
    from analytics_generator.analytics_generator import AnalyticsGenerator
  File ".../project/pseudo/analytics_generator/analytics_generator.py", line 1, in <module>
    from models.blueprints import SomeClass
ModuleNotFoundError: No module named 'models'

I'm running the script within Pycharm and my working directory is .../project/pseudo

In the analytics_generator.py file, if I change the import statement to a relative import it works: from .models.blueprints import SomeClass.

However, using the full path doesn't:

from pseudo.analytics_generator.models.blueprints import SomeClass throws:

ModuleNotFoundError: No module named 'pseudo.analytics_generator'; 'pseudo' is not a package

Any guidance is much appreciated!

martineau
  • 119,623
  • 25
  • 170
  • 301
Julian Mclain
  • 21
  • 1
  • 3
  • Have you read [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time)? – martineau May 05 '19 at 20:44

1 Answers1

0

The directory in which the script is being executed shouldn't need to be specified. Since you're executing __main__.py, the following should do:

from analytics_generator.models.blueprint import SomeClass

Source/Further Reading: The Definitive Guide to Python import Statements: Absolute vs. Relative Import

Example Directory Structure

   test/                      # root folder
        packA/                 # package packA
            subA/              # subpackage subA
                __init__.py
                sa1.py
                sa2.py
            __init__.py
            a1.py
            a2.py
        packB/                 # package packB (implicit namespace package)
            b1.py
            b2.py
        math.py
        random.py
        other.py
        start.py

For example, suppose we are running start.py which imports a1 which in turn imports other, a2, and sa1. Then the import statements in a1.py would look as follows:

  • absolute imports:

      import other
      import packA.a2
      import packA.subA.sa1
    

Note that there isn't a need to specify import test.other or import test.packA.test.other (where test is the directory in which the script start.py is being executed). The same principle should apply to your situation, regardless of __init__.py or not, given you're using Python 3.3 and above.


For posterity and completeness, I'll quote another section of the guide:

[...] when Python runs a script, its containing folder is not considered a package.

This explains the 'pseudo' is not a package error.

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • 1
    Thank you! Your suggestion solved the issue, and the example you provided is very similar to the situation I'm working with. I will give the rest of the tutorial a read. – Julian Mclain May 05 '19 at 20:00
  • One thing that I find weird is that Pycharm doesn't validate the import statement. I get ```Unresolved reference 'analytics_generator' more... (⌘F1)``` even though the script runs without error.... @TrebledJ Do you have any experience here? More of an IDE thing so I'll read up on the Pycharm docs – Julian Mclain May 05 '19 at 20:07
  • @JulianMclain Hehe, yeah I get those warnings on PyCharm too, I ignore them for the most part (probably because I've only ever made/use one package in my entire history). I'll be honest—I'm only an amateur with PyCharm. :) – TrebledJ May 05 '19 at 20:11