How to activate/deactivate a virtualenv from python code? - python

For activation there is a script that activates a virtualenv from an already running python interpeter using execfile('C:/path/to/virtualev/Scripts/activate_this.py', dict(__file__='C:/path/to/virtualev/Scripts/activate_this.py')). However since I can still import packages that are not in the virtualenv from the current python script I am confused about how it works.
For deactivation there is no python script at all.
What should I do?

From part of the VirtualEnv homepage.
You must use the custom Python interpreter to install libraries. But
to use libraries, you just have to be sure the path is correct. A
script is available to correct the path. You can setup the environment
like:
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

If you want to run a program outside of the virtualenv, just run your system python executable (e.g. /usr/bin/python) instead of the one in the virtualenv.

This souds like bad idea. You are trying to modify environment of your script within this script. Please explain why?
Can't you do it hierarchical? Use one script to run different scripts in different virtualenvs.

at the command line, type the word 'deactivate'

Related

What should my path if I am using python venv?

I use python 3.6.9 and asterisk 16.16.0 in Ubuntu 18.04.
If you know about AGI then you probably know about this line
#!/usr/bin/python
or this
#!/usr/bin/env python
if not then don't worry its tells the system to run this script in the Python interpreter.
So as far you know what I can do. but now my question is if I am use virtual environment for python using help of venv package and want defines path of that python environment in my AGI then what should I define.
My virtual environment store in following directory: /home/test/Documents/environments/test_env.
When you are running ANY agi script in asterisk it will run under asterisk user.
Asterisk user on most systems is limited user and may have no python environment at all.
Best way is create virtual environment for your python and use full path for your python INSIDE venv like /opt/my_virtual_env/bin/python3 in all agi scripts.
Ensure that those path is readable by asterisk user or owned by asterisk user
Try adding this path to the $PATH variable
/home/test/Documents/environments/test_env/bin
This directory contains scripts like activate, deactivate etc. which are used for activating and deactivating the virtual env.
Regarding the shebang, #!/usr/bin/env python should work properly if the virtual env is activated.
Hope this is what you were looking for.

Run python Script as root

I have a python Script when i run my script by using my account it's run without problem ,
But when i use the root account i have an issue like this
I don't know why i have a problem with import module
use an absolute python path to point to the version of python that has the right packages installed.
The command which python executed as the user where the script works should help.
If you used pip to install any package locally, then you might have to
ensure as well, that the environment variable $HOME points to /home/robot.
In general I'd recommend to create a virtualenv foe your specific python project and use it in the sudo call explicitely or if you refer change the #! line in your script to point to the right virtualenv,

Python, virtualenv: telling IDE to run the debug session in virtualenv

My project's running fine in virtualenv. Unfortunately, I can't yet run it via my IDE (Eric) because of the import troubles. It stands to reason, as I never told the IDE anything about virtualenv.
I know the drill ($ source project/bin/activate etc.), but lack general understanding. What constitutes "running inside virtualenv"? What IDE options might be relevant?
I think the only required setting to run or debug code is path to python interpreter.
Relevant IDE options could be SDK or Interpreter settings.
Note that you should run not default python (eg. /usr/bin/python) but python binary in your virtual environment (eg /path/to/virtualenv/bin/python)
Also there are some environment variables set by activate, but i think they aren't needed when you point to virtualenv python binary directly.
So, again, what activate does is only environment variables setup: at least, it modifies system $PATH in a way that python and pip commands points to executable files under path/to/virtaulenv/bin directiry.
As far as I know it is possible to run your script/project using your virtualenv simply by calling /path/to/your/venv/python your_script.py. To install new packages in your venv for example, you would run /path/to/your/venv/pip install some_package.
I guess the main advantage of "runnnig inside virtualenv" would be not being concerned about having to inform the location of python packages/executable everytime you want to run some script. But I lack general understanding too.
I usually install a virtualenv with the --no-site-packages option in order to have a "clean" installation of python.
--- EDIT ---
The second answer of this discussion has a nice explanation.
Simply look at the project/bin/activate, everything you need is there to set up the appropriate search.
Usually the most important path is the PYTHONPATH which should point to the site-packages.

How to run gwan with python in virtual environment?

My python script is as below
#!~/PyEnv/bin/python
import sys
import my_lib
print 'hello'
# do something with my_lib
my_lib()
sys.exit(200)
I placed it in /csp folder with name 'hello.py'. When I connected to "localhost:8080/?hello.py", I received a message "ImportError: No module named my_lib".
Because this script didn't run with python in virtualenv. How can I resolve it ?
This should be a problem with the local path. VirtualEnv is just a setup tool that can generate local python environment. This is used a lot for isolating a project from the system python.
I think that when you use the path ~/PyEnv/bin/python, then this version of python doesn't automatically redirect import requests to ~/PyEnv/lib.
This is some PATH issue and I am not sure if G-WAN should necessarily address this :)
Let's try to narrow the cause of the problem:
Does the G-WAN Python example run fine WITHOUT virtualenv?
Does the G-WAN Python example run fine WITH virtualenv?
Does your script work without virtualenv?
If the latter is true then you might need to investigate what virtualenv is doing.

Why can't python find some modules when I'm running CGI scripts from the web?

I have no idea what could be the problem here:
I have some modules from Biopython which I can import easily when using the interactive prompt or executing python scripts via the command-line.
The problem is, when I try and import the same biopython modules in a web-executable cgi script, I get a "Import Error"
: No
module named Bio
Any ideas here?
Here are a couple of possibilities:
Apache (on Unix) generally runs as a different user, and with a different environment, to python from the command line. Try making a small script that just prints out sys.version and sys.prefix, and compare the result through apache and via the command line, to make sure that you're running from the same installation of python in both environments.
Is Biopython installed under your home directory, or only readable just for your normal user? Again, because apache generally runs as a different user, perhaps you don't have access to that location, so can't import it.
Can you try doing import site before trying to import Biopython? Perhaps something is preventing site packages from being imported when you run through apache.
In the cgi script, you could try to add the path to this package before any import.
sys.path.insert(0, 'path to biopython package')
If you are using Apache, you should be able to set the PYTHONPATH in conf file with directive SetEnv
SetEnv PYTHONPATH "path to biopython package"
I had same problem. I solved this problem by changing user of Apache in Linux Ubuntu by command in terminal:
sudo gedit /etc/apache2/envvars
Please change www-data on export APACHE_RUN_USER and export APACHE_RUN_GROUP to your current user or user that can run python script.
Have a good time ;)

Categories