1

I am working on learning Django by making a simple analysis dashboard. I did the startproject command and the startapp command like the tutorial ran through. I added a new file called connectionOracle.py in the app folder. My folder structure is (top folder is was created via venv)

AnalysisSite
|AnalysisSite
|coreAnalysis
||models.py
||connectionOracle.py

I have a class called WorkOrder in the models.py file. I am trying to import it into the connectionOracle.py file by doing the following

from .models import WorkOrder

I then go to the Command Line (on windows) and navigate tot he coreAnalysis folder and run the following

python connectionOracle.py

I get an error that says.

ImportError: attempted relative import with no known parent package

I did some reading online, and I tried doing an absolute path with AnalysisSite.AnalysisSite.coreAnalysis.models that didnt work. I also tried moving the connection file to different directories and that didnt work either. I also tried going into the command line and typing set DJANGO_SETTINGS_MODULE = AnalysisSite.settings

I also put a _init_.py in each folder. (Django automatically put it into the project directory and app directory).

I am not sure what I am doing wrong

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • `from coreAnalysis.models import WorkOrder`. – Willem Van Onsem Aug 30 '21 at 19:29
  • I had tried this as well. I got the error 'ModuleNotFoundError: No module named 'coreAnalysis'' I double checked the spelling and in different cases(upper/lower). I also tried to see if it was an issue with the apps.py files, but the name was set to coreAnalysis – Zain Kinnare Aug 30 '21 at 19:33
  • do you have `__init__.py` files in the root directory and for the `coreAnalysis` directory? – Willem Van Onsem Sep 02 '21 at 11:37

1 Answers1

0

you are trying to access Django components(models) by a script file , the caller in this case not Django itself ( the request not coming from url or different django tools or mechanism),

anyway in your custom python file which is 'connectionOracle.py' try to do some steps before accessing the models itself,

the steps are available on the following URL:

https://stackoverflow.com/a/68936419/12662056

############

change the path for project depending on your project path.

i hope this helpful

K.A
  • 1,399
  • 12
  • 24
  • I tried this and ended up getting the following error : ModuleNotFoundError: No module named ' AnalysisSite' . I tried some different variations of the path. I also attempted to path it directly to the coreAnalysis folder and i got the same error (it said AnalysisSite was not named). – Zain Kinnare Aug 30 '21 at 20:27