无法理解python虚拟环境

时间:2014-11-10 19:39:14

标签: virtualenv

我创建了一个python虚拟环境来使用这些指令运行应用程序:

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

我正在尝试在托管其他2个网站的远程服务器上进行设置。

我还没有让它正常工作,但我不明白的是这个

虚拟环境必须持续运行?

如果我关闭远程连接,或退出环境,应用程序是否停止运行?

如果不是,我该如何退出虚拟环境并继续在服务器上工作?

1 个答案:

答案 0 :(得分:1)

虚拟环境不是需要运行的东西。它基本上是一个可以安装Python库和可执行文件的目录,还有一些environment variables来确保:

  • 新库安装在虚拟环境中
  • 当Python程序查找库时,它会在虚拟环境中查找
  • 当系统查找要运行的程序时,它首先在虚拟环境中查找。

激活虚拟环境时发生的一件事是它定义了一个名为deactivate的shell函数,该函数取消设置所有环境变量。因此,要离开虚拟环境,只需键入deactivate

  

如果我关闭远程连接,或退出环境,应用程序是否停止运行?

这取决于您如何启动应用程序。如果您只是从命令行启动它,那么当您关闭连接时,应用程序将停止。通常,您希望使用upstart之类的服务来启动和管理您的应用程序(您选择的特定服务通常由服务器的操作系统决定)。配置该服务时,您需要确保它在启动应用程序之前运行source $your_environment_dir/bin/activate,以便您的应用程序可以在虚拟环境中运行。

相关问题