Stan Smith
Stan Smith

Reputation: 69

Trouble understanding python virtual enviornment

I have created a python virtual enviornment to runn an applicaton using these instructions:

git clone http://github.com/MediaCrush/MediaCrush && cd MediaCrush

Create a virtual environment

Note: you'll need to use Python 2. If Python 3 is your default python interpreter (python --version), add --python=python2 to the virtualenv command.

virtualenv . --no-site-packages

Activate the virtualenv

source bin/activate

Install pip requirements

pip install -r requirements.txt

Install coffeescript

npm install -g coffee-script

Configure MediaCrush

cp config.ini.sample config.ini

Review config.ini and change any details you like. The default place to store uploaded files is ./storage, which you'll need to create (mkdir storage) and set the storage_folder variable in the config to an absolute path to this folder.

Compile static files

If you make a change to any of the scripts, you will need to run the compile_static.py script.

python compile_static.py

Start the services

You'll want to make sure Redis is running at this point. It's probably best to set it up to run when you boot up the server (systemctl enable redis.service on Arch).

MediaCrush requires the daemon and the website to be running concurently to work correctly. The website is app.py, and the daemon is celery. The daemon is responsible for handling media processing. Run the daemon, then the website:

celery worker -A mediacrush -Q celery,priority
python app.py

This runs the site in debug mode. If you want to run this on a production server, you'll probably want to run it with gunicorn, and probably behind an nginx proxy like we do.

gunicorn -w 4 app:app

I am trying to set this up on a remote server which is hosting 2 other websites.

I haven't actually got it to work properly yet, but what I don't understand is does this

virtual environment have to be running continuously?

If I close my remote connection, or exit the environment does the application cease to function?

And if not how do I exit the virtual environment and continue to work on the server?

Upvotes: -1

Views: 113

Answers (1)

Erin Call
Erin Call

Reputation: 1784

The virtual environment isn't something that needs to be running. It's basically a directory where Python libraries and executables can be installed, and a handful of environment variables to ensure that:

  • new libraries are installed in the virtual environment
  • When a Python program looks for a library, it looks in the virtual environment
  • When the system looks for a program to run, it looks in the virtual environment first.

One of the things that happens when you activate the virtual environment is it defines a shell function called deactivate that unsets all the environment variables. So, to get out of the virtual environment, you just type deactivate.

If I close my remote connection, or exit the environment does the application cease to function?

It depends on how you've started your application. If you are just launching it from the command line, then when you close your connection the application will be stopped. Typically you want to use a service like upstart to start and manage your application (the particular service you choose is typically determined by your server's OS). When you configure that service, you'll want to make sure it runs source $your_environment_dir/bin/activate before starting your app, so that your app will run in the virtual environment.

Upvotes: 1

Related Questions