0

How to import file from parent directory in python?

My project tree is as follows:

enter image description here

From __main__.py file which is in vehiclesListCrawler I want to import common.py which is in a parent directory.

Now I do it as follows:

from common import get_init_url, create_urls_to_fetch, MILEAGE_RANGES, notify_on_error

But I'm getting an error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "./__main__.py", line 11, in <module>
    from common import get_init_url, create_urls_to_fetch, MILEAGE_RANGES, notify_on_error
ModuleNotFoundError: No module named 'common'

Any idea?

UPDATE

Nothing from here didn't help. That is why I've posted the new question.

Inside __main__.pyI have main function as follows:

def main(params):
    try:
        process.crawl(AutoscoutListSpider, params)
        process.start()
        return {"Success ": main_result}
    except Exception as e:
        return {"Error ": e, "params ": params}

And I run it as follows:

main({"make":"Audi", "model":"A3", "mileage":"2500"})
Boky
  • 11,554
  • 28
  • 93
  • 163

1 Answers1

-1

Way 1: absolute import:

from asCrawlers.common import get_init_url, create_urls_to_fetch, MILEAGE_RANGES, notify_on_error

Way 2: relative import:

from ..common import get_init_url, create_urls_to_fetch, MILEAGE_RANGES, notify_on_error

More on imports here:

When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc.

Eugene Primako
  • 2,767
  • 9
  • 26
  • 35
  • First one gave me the same error `ModuleNotFoundError: No module named 'asCrawler'`. And the second one `ImportError: attempted relative import with no known parent package` – Boky Jul 05 '18 at 11:56
  • @Boky Not "asCrawler", but "asCrawlers" - didn't you mistype it? – Eugene Primako Jul 05 '18 at 11:58
  • I mistyped it here. I've copied your code. The same problem – Boky Jul 05 '18 at 11:59
  • @Boky How do you run this code? If you try to run ```__main__.py``` as a script, then scharette is right and the answer can be found in linked question (like https://stackoverflow.com/a/11158224/747744). – Eugene Primako Jul 05 '18 at 12:04
  • @Boky Could you please show exact code and error when updating ```sys.path``` before import? – Eugene Primako Jul 05 '18 at 12:28