简体   繁体   中英

Bash scripts. How to select python environment?

every time I need to start a Django server, I have to do the following:

cd to some directory
source env/bin/activate
python manage.py run_gunicorn 0.0.0.0:8000

How could I simplify this process by creating a script that would start the server once executed.

I tried to create .sh script with, but I failed to change Python environment with source command.

Then I tried to create .py file and execute commands with os.system() , but virtualenv did not seem to have been initiated.

I would do it as a shell function rather than as a script:

djangoserver() {
    cd $1
    source env/bin/activate && python manage.py run_gunicorn 0.0.0.0:8000
}

This should be functionally equivalent to running it at the command line.

You could put the same lines inside a shell script, in which case the 'source' command would be local to the shell script. That would probably be a good thing, as long as there aren't variables that you want to keep in scope after you run the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM