how to use python to do a series of commands in terminal
Question: I put the following in my `python_go.py`

    
    
    import os
    os.system("cd some_dir") # This is the directory storing an existing virtual environment
    os.system(". activate") #because I want to activate the virtual environment
    os.system("cd another_dir") #this the directory I can start my work
    

I hope I can run the `python_go.py`, it can do the work mentioned above.

But when I run it, seems it can only do the first step, the rest of it, e.g.
`. activate` seems not working.

Can someone tell me how to do it? Thank you!!


Answer: If you're relying to os.system(". activate") to work if it's in the directory
some_dir that won't work because the current directory won't persist across
calls to os.system().

You're going to be better off calling a shell script that aggregates all three
of the commands you want to do and execute that once from the python script.

Otherwise you want to set up the environment for the parent python process
using os.chdir() before calling os.system on the activate call. Also, the
os.system(". activate") call won't do what you want because the "dot space"
notation will load information into a shell that's going to go away when the
os.system call finishes.

Edited (to your followup comment):

Your shell script should look like this (do_activate.sh):

    
    
    cd some_dir
    . activate
    cd another_dir
    

and the python code like this:

os.system("db_activate.sh").

Keep in mind that whatever environment variables were saved by ". activate"
won't persist after the os.system call.

