0

After starting my app using dev_appserver.py --enable_console true my_app/, I go to localhost:8000, select the interactive console, and then run a Python script that initializes the datastore.

Is there a way to run this init script from the command line?

I looked at --python_startup_script my_init_script.py, but that is called before the app is started, and so it doesn't make sense.

feklee
  • 7,555
  • 9
  • 54
  • 72
  • 1
    Don't know if that is possible but what about putting this code at the beginning of your ```main.py``` or whatever is your main file. The process then becomes - your app starts, loads your main file (maybe when someone visits your home page) which checks if a flag is set. If flag is not set, it runs your datastore init script and sets the flag (maybe the flag is set in datastore itself). I had something similar to what I described but the code is triggered when you try to access the home page url – NoCommandLine Mar 10 '22 at 21:03
  • @NoCommandLine I implemented the same approach now, seems to work fine. Calling the init script can be done using `execfile('my/init/script.py')` when using Python 2.7. The only change that I needed to do was remove a `print` statement, or else the HTTP response which triggers initialization would be meesed up. Consider turning your comment into an answer, because that's what it is. – feklee Mar 11 '22 at 15:05
  • Have you checked this public [document](https://docs.google.com/document/d/1CCSaRiIWCLgbD3OwmuKsRoHHDfBffbROWyVWWL0ZXN4/)? The `--python_startup_script` flag will run the script **after** starting the process. I haven’t tested this yet to check if it works like that. – ErnestoC Mar 11 '22 at 15:26
  • @ErnestoC It says "before any user code is executed.", which makes sense. It wouldn't even know when the user code has finished setup and is ready to receive initialization data. Before that occured to me, I tried `--python_startup_script`, and it just did nothing. – feklee Mar 11 '22 at 15:32

1 Answers1

2

Moving details from comments section to full answer

Don't know if that is possible but a possible workaround could be to put this code at the beginning of your main.py or whatever is your main file.

The process then becomes - your app starts, loads your main file (maybe when someone visits your home page) which checks if a flag is set. If flag is not set, it runs your datastore init script and sets the flag (maybe the flag is set in datastore itself).

I had something similar to what I described but the code is triggered when you try to access the home page url.

NoCommandLine
  • 5,044
  • 2
  • 4
  • 15