1

I am trying to call a function from a python file that is located outside the django project directory from views.py. I have tried importing the python file but running the django server says "no module named xxxx".

Tree structure is given below.


├── auto_flash
│   ├── __init__.py
│   └── test.py
└── fireflash
    ├── detection_reports
    │   ├── admin.py
    │   ├── admin.pyc
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── migrations
    │   │   ├── __init__.py
    │   │   └── __init__.pyc
    │   ├── models.py
    │   ├── models.pyc
    │   ├── __pycache__
    │   │   └── __init__.cpython-35.pyc
    │   ├── templates
    │   │   └── detection_reports
    │   │       └── home.html
    │   ├── tests.py
    │   ├── views.py
    │   └── views.pyc
    ├── fireflash
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── __pycache__
    │   │   ├── __init__.cpython-35.pyc
    │   │   └── settings.cpython-35.pyc
    │   ├── settings.py
    │   ├── settings.pyc
    │   ├── ui_cgi.py
    │   ├── urls.py
    │   ├── urls.pyc
    │   ├── wsgi.py
    │   └── wsgi.pyc
    ├── __init__.py
    └── manage.py  

Project name here is "fireflash" and there is an app in that project named "detection_reports". There is another directory named "auto_flash" that has same location as "fireflash". What I want to do is to import test.py file from "auto_flash" in detection_reports.views and call a function from views. Importing like this "from auto_flash import test" throws error "no module named auto_flash".

Asad Ali
  • 11
  • 1
  • 4
  • 1
    Can you provide a "file tree" with the relevant files (and their directories)? – Willem Van Onsem Dec 24 '18 at 19:01
  • 2
    Possible duplicate of [How import a function from another view with Django?](https://stackoverflow.com/questions/47676426/how-import-a-function-from-another-view-with-django) – sahasrara62 Dec 24 '18 at 19:17
  • A similar question was posted [here](https://stackoverflow.com/questions/36622183/how-to-import-a-function-from-parent-folder-in-python) – Alouani Younes Dec 24 '18 at 23:45
  • I have tried the solutions suggested above but none of them worked. Anyone with clean/proper solution.? – Asad Ali Dec 25 '18 at 11:58

2 Answers2

0
  • Move auto_flash to fireflash
  • In detection_reports add from auto_flash import test.py
dKen
  • 3,078
  • 1
  • 28
  • 37
0

I have tried all of the above mentioned solutions, but the cleaner solution was to append the path for auto_flash to syspath and the issue was resolved. Thanks to all of you for the efforts.

dKen
  • 3,078
  • 1
  • 28
  • 37
Asad Ali
  • 11
  • 1
  • 4