1

My question is very similar with this one. The difference in my case is that the python script I want to start as a subprocess (listener1.py), is part of my project, just on a different app.

This is my project structure:

my_project
    ├── my_app1 #This app contains models,views etc.
    │   ├── models.py
    │   └── apps.py
    │   └── ...
    ├── logic_app #This app contains several unrelated python scripts
    │   └── ManagerScript.py
    │   └── listener1.py
    │   └── ...
    

In my apps.py:

class MyAppConfig(AppConfig):
    name = 'my_app1'
    def ready(self):
        #Once app is ready, start the following script as a thread
        thread = Thread(target=ManagerScript.main)
        thread.start()

In ManagerScript.py:

#This thread is responsible for starting listener1.py as a Subprocess
#(because this is a thread I can successfully import my Models here)
#from my_app1 import models #This would work!
        
tmp =  ['python3','path/to/listener1.py']
Popen(tmp, preexec_fn=os.setpgrp)

In listener1.py:

from my_app1 import models #Does not work!
...

My question is, considering the fact that the subprocess listener1.py is in logic_app folder, how could I import my Models in listener1.py script without using Django Management Commands? (Because using them would require to change my current project structure)

1 Answers1

0

You can run them through the Django shell. Without writing it as a command.

Python manage.py shell
exec(open(<path-to-file>).read()))
TeRe
  • 91
  • 2
  • The `listener1.py` should automatically start once the `my_app1` is ready. Could I integrate your solution on `ManagerScript.py` ? – ChrisChris2323 Apr 04 '22 at 14:35