Can I run Apache, mod_wsgi and MongoDB within virtualenv? - apache

I'm learning about virtualenv and mod_wsgi and these are my favourite articles so far:
https://code.google.com/p/modwsgi/wiki/VirtualEnvironments
(by the author of mod_wsgi, Graham Dumpleton).
http://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/
I understand that virtualenv allows me to have independent instances of Python and Python packages.
But how does this relate to things like Apache, mod_wsgi and MongoDB?
Can these things also be included in this virtual environment?
The scenario I am interested in is being apple to have a local application that is 'self reliant' (that would be easy to install on other systems) - virtualenv seems to enable this to an extent, but can Apache, mod_wsgi and MongoDB only exist at this broader 'system level' or can they exist in a virtual environment?

virtualenv is for python packages only, so you cannot have instances of system applications running within in it, like apache.
Edit: As mentioned by #Graham Dumpleton, it is possible to install apache httpd using mod_wsgi-httpd inside virtualenv. But, as I said earlier virtualenv is for python packages, so it is a matter of finding/writing a package that solves the problem.
That being said, you can make Apache, that uses mod_wsgi, to take advantage of virtualenv. Trick is, configuration has to be done within a python file.
Say you are trying to setup django project myproject, that is running within virtualenv named myproject, here is your wsgi.py:
import os, sys, site
path = os.path.split(os.path.dirname(__file__))[0]
if path not in sys.path:
sys.path.append(path)
site.addsitedir('~/.virtualenvs/myproject/local/lib/python2.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
activate_env=os.path.expanduser(
"~/.virtualenvs/myproject/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Configuration for apache or mongodb wouldn't be any different, since they are system wide running services.

Related

Django runserver_plus Werkzeug not installed error, although it is

I am trying to run my Django application using runserver_plus as I am using an SSL certificate to create HTTPS connections to the backend.
In order to do so, I have done the following:
pip install django_extensions
Installed Django extensions to the project but include
"django_extensions" in the "INSTALLED_APPS" list in the settings.py
file.
pip install Werkzeug
python -m pip freeze (to make sure everything has been installed
correctly within the venv)
ran ./manage.py to check the available commands (to make sure Django
extensions is working as expected)
However, I get the following error:
CommandError: Werkzeug is required to use runserver_plus.
although everything has been installed properly and all checks/re-installs have been made. Also, it must be noted that I updated Django, django_extensions, and Werkzeug to the latest versions available.
This is a recurrent issue with Werkzeug. Your safest option is to downgrade to 0.16.1.

Removed Python, can I still use the the virtual environment?

I created a virtual environment using python3.9 -m venv myenv
Now I activate it using source myvenv/bin/activate.
I suppose from this virtual enviroment is some dependency or link created to the installed Python version.
When I uninstall python3.9 can I still launch my python script with this activated virtual enviroment?
no you wont be able to run python applications anymore.
refer https://docs.python.org/3/library/venv.html
venv — Creation of virtual environments¶
New in version 3.3.
Source code: Lib/venv/
The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.
See PEP 405 for more information about Python virtual environments.
If deleting the installed Python version affects the further use of any virtual environment depends wether option symlinks or copies was used when the virtual environment was created.
From Python docs for module venv, Creating virtual environments:
It also creates a bin (or Scripts on Windows) subdirectory containing a copy/symlink of the Python binary/binaries (as appropriate for the platform or arguments used at environment creation time).
Emphasis mine, to note that the role of the installed Python version depends on options.
The installed Python version is still a dependency, when at the time of creation symlinks have been used. If copies have been used, then the virtual environment is independent of the Python installed version and it can be removed.
See the options:
--symlinks Try to use symlinks rather than copies, when symlinks
are not the default for the platform.
--copies Try to use copies rather than symlinks, even when
symlinks are the default for the platform.
See also: PEP 405: Copies versus symlinks

Cannot run standalone psychopy if PYTHONPATH and PYTHONHOME set for different python version

I thought that the standalone PsychoPy install could coexist happily if Python was installed separately on the PC to but I can't get it to, nor can I find any docs. (I'm using Windows 7)
I have the lastest standalone version installed and the shortcut to run it is
"D:\Program Files (x86)\PsychoPy2\pythonw.exe" "D:\Program Files (x86)\PsychoPy2\Lib\site-packages\PsychoPy-1.81.02-py2.7.egg\psychopy\app\psychopyApp.py"
This works fine if my system env variables for PYTHONHOME & PYTHONPATH aren't set but I also use Python for other apps and need them setting to point to the other version of Python I have installed natively. When these env vars are set, Psychopy fails to load and gives no error messages at all.
Can anyone advise how I get them to play together nicely? (I thought it used to work last year, has something changed?)
[ I've tried a full uninstall of psychopy and freshly installed the latest standalone version v1.81.02
Yes, this is an unfortunate consequence of the way that PsychoPy is currently bundled with it's own closed environment in it's own python and dependencies installed seperately.
However, a new option to install psychopy using the conda package manager was introduced recently for Mac OS but some have also got it to work on Windows with a bit of tweaking.. Work is currently ongoing for this feature. I doubt that it was working previously unless you manually installed all dependencies in your default python, or ran linux:
On linux you can simply install psychopy from the neuro.debian repository, making it available for python system-wide. See PsychoPy documentation.
Thinking about it, I don't think it would ever worked if you had set PYTHONPATH (I don't know about PYTHONHOME).
BUT I did have a 'regular' python installation running alongside my Standalone PsychoPy install by not using the PYTHONPATH variable. You can add further paths to your python importing path (I assume that's the aim here) without setting any environment variable by adding text files ending in .pth to your site-packages directory. Essentially any lines in a .pth file that is found while navigating the existing path will also be added to the path!
Actually, according to the python docs you can also set a flag -E to ignore the environment variables:
https://docs.python.org/2/using/cmdline.html
To use that solution for the Standalone PsychoPy installation you'd have to alter the application shortcut to add this (that should get the app to load), but also make a couple of changes to the code for running scripts so that they also run with the flag set.
I still think not setting those variables is the easier solution though.
cheers,
Jon

Why does apache2.2 keeps installing instead of apache2.4?

I've been trying to set Vagrant up for local development on my machine. I see from Apache's official website that 2.4.7 is the current stable version. And I had no issues installing it on my VPS. But with Vagrant it's a different story. I'm trying with both Base64 and Base32 boxes. This is the command i'm running:
sudo apt-get install -y apache2
It should install apache 2.4.x. But it doesn't. Sometimes it installs version 2.2, sometimes 2.4. This is driving me nuts, since the whole idea of using Vagrant is to have the same Virtual machine replicated everywhere. Any ideas?
Because the Debian based distro (possibly Ubuntu) you're using is still using 2.2.x. Switching to 2.4.x from 2.2.x means all the 3rd party modules they ship have to be upgraded (or at least rebuilt) so they probably haven't made the switch yet.

Run linux distro of choice inside existing distro

Just wondering if it's possible and what the best route might be to run a full-on Linux distro within my existing distro? It would be great to for instance run Arch Linux within a chroot, jail, etc.. I believe people are doing this on Chromium for example.
I would require that whatever fs loaded, I can install packages using pacman and that my changes are kept intact.
I have tried the Virtualbox route by the way and there is a pretty nasty bug involving double mouse pointers on rotated host screens that I can't seem to get around.
I should mention that I'll be using this chroot environment for development, maybe running the odd X client to be exported remotely, etc..
I followed the chroot guide at https://wiki.archlinux.org/index.php/Change_Root and basically installed a whole Arch system within a nested chroot according to the Arch Linux installation guide and I'm now able to switch to the environment at will.
There is a tool https://github.com/fsquillace/junest that does everything automatically for you (downloads and unpacks ArchLinux distro inside some folder and chroots there).