pythonghost-blog

Run Ghost blog locally with python script


I have installed ghost blog locally on Windows 10.

To run ghost, I run the following the commands manually on Windows prompt;

$ C:\Users\johnK\Dropbox\jk\ghost
$ ghost start

I want to automate this with a Python script. This is how my Python script looks.

import os
    
os.system(r"C:\Users\johnK\Dropbox\jk\ghost")
os.system("ghost start")

Unfortunately, I get the following error;

'C:\Users\johnK\Dropbox\jk\ghost' is not recognized as an
internal or external command, operable program or batch file.
Working directory is not a recognisable Ghost installation.
Run `ghost start` again within a folder where Ghost was installed
with Ghost-CLI.

I am using Python v3.9, Windows 10


Solution

  • To be able to replicate the behavior you need first understand what you are doing in your Windows prompt:

    $ C:\Users\johnK\Dropbox\jk\ghost
    

    means you change the working directory to be C:\Users\johnK\Dropbox\jk\ghost (it's kinda shortcut for cd C:\Users\johnK\Dropbox\jk\ghost) and then:

    $ ghost start
    

    executes ghost executable (.bat, .exe whatever it is) located in that directory and passing start as invocation argument. So in fact your ghost executable could theoretically be run with one-liner:

    $ C:\Users\johnK\Dropbox\jk\ghost\ghost start
    

    To mimic that in your python script you can either try to just run above oneliner or first change current working directory for Python and then run your executable:

    import os
    
    os.chdir(r"C:\Users\johnK\Dropbox\jk\ghost")
    os.system("ghost start")