4

So I have tried many solutions found on stack overflow but am still having issues.

Below is my project structure.

project
    | tool1
       | - application.py
    | resources
       | - tools
          | - dao.py

Within application.py I have the following import statement:

from resources.tools.dao import DAO

This works fine in PyCharm, but when I try to run it from the command line from the project directory (python tool1/application.py), I get:

ModuleNotFoundError: No module named 'resources'

I get the same error if I move into 'tool1' folder and run: python application.py

I have tried adding .. before the imports, setting different folders as my source root folder through PyCharm, and adding blank __init__.py files to the resources directory.

Why is PyCharm able to find 'resources' but the command line is not? How can I change this so that my solution works from either location?

If it helps, I am using Python 3.7 and Windows. I am open to restructuring the project directory, but there are going to be multiple tools/applications that will all use dao.py and the other tools in the resources directory, so resources should be at least as high level as the other projects.

Thank you in advance.

2
  • any updates on this? Commented Nov 21, 2019 at 14:10
  • 1
    multiple good answers below. ended up using something most similar to the accepted answer. Commented Nov 21, 2019 at 18:42

5 Answers 5

2

Run the script using the -m argument. i.e.

python -m project.tool1.application

This makes the script run with the project package in the namespace. (You may need to add an __init__.py file to project to make it a package)

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you created __ init __.py in each directory. You could add to system path. It should import dao from anywhere. It works in linux for sure.

# tool1.__init__.py
import sys
import os

CURRPATH, TAIL = os.path.split(os.getcwd())
while CURRPATH != "/":
    if TAIL == 'project':
        if os.path.join(CURRPATH, TAIL) not in sys.path:
            sys.path.append(os.path.join(CURRPATH, TAIL))
        break
    CURRPATH, TAIL = os.path.split(CURRPATH)
from resources.tools.dao import DAO

and in application.py

# tool1.application.py
from __init__ import DAO

3 Comments

This worked for me, it just doesn't seem too elegant because you have to go into the init to see exactly where the dao is. I would prefer if I could somehow do from resources.tools.dao import DAO. Do you know of a tweak to accomplish that? ?
Just copy code from init into application.py and eliminate "from init..."
too involved. some of the other answers may be less of an eye-sore
1

Another way to declare the parent directory path could be:

import os, sys

dir_path = os.path.dirname(os.path.realpath(__file__))
parent_dir_path = os.path.abspath(os.path.join(dir_path, os.pardir))
sys.path.insert(0, parent_dir_path)

And then this line would work without issues:

from resources.tools.dao import DAO

Comments

0

If project is your root directory (i.e. you have initialized pythonpath to this directory), then you need to import modules like this:

from project.resources.tools.dao import DAO

2 Comments

is there an elegant way to initialize the pythonpath to the root folder programmatically?
yes, you have to run : export PYTHONPATH=Your_root_folder:$PYTHONPATH. This will set your project under python paths.
0

I encountered a similar issue where I mistakenly included the databricks comment that declares a notebook (# Databricks notebook source) in the file I tried to import from. Removing that line resolved the problem for me.

Thought it might help others.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.