python: os.getenv returns none with sudo? - python

I've set environment variable LIBRARY_PATH in /etc/bash.bashrc by adding export LIBRARY_PATH=/usr/local/cuda/lib64:$LIBRARY_PATH at the end.
When I try to get the env-variable from python:
ipython
import os
print os.getenv('LIBRARY_PATH')
Everything works well, it prints /usr/local/cuda/lib64:.
BUT when I invoke ipython with sudo:
sudo ipython
import os
pront os.getenv('LIBRARY_PATH')
I get nothing. I guess this is about env-variables accross users, but what is the ditails? I set LIBRARY_PATH in /etc/bash.bashrc which is said to be the 'system wide bashrc file'.
So how can I get the correct env-variable with sudo in python ?

If you want sudo to pass through environment variables (which is generally considered a security hazard), use sudo -E.
Note that it is bash which executes the commands in the bashrc files. ipython is not bash, obviously, and sudo does not start up a shell process, much less a bash process, just in order to run the command you are requesting it to run. So none of your bashrc files are going to be executed by the sudo command or in the sudo subprocess. Of course, you can tell sudo to run a bash process:
sudo bash -c ipython
However, bash does not execute startup files if it detects that it is being run in a sudo process.
For more information on how sudo cleans the environment, type man 5 sudoers and skip down to the Command environment section.

If you set the environment variable in /root/.bash_profile, then you will probably get the desired behavior when using sudo.

Related

Activating conda environment in bash script that runs on startup

So I have a python script that generates an animation - and it requires libraries that I have in a conda environment. I need to run this script as soon as my computer turns on, so I've written a short bash script that I added to "startup applications". This bash script runs on startup, and reads like this:
#!/bin/bash
conda activate myenv
cd ~/scripts
python generate.py
When I run this in terminal myself, it's fine, but whenever I turn on the computer, the python part of the script doesn't execute, and when I check errors i find:
conda: command not found
and then i also see the python script failed to run because it's missing libraries (from the conda environment not activating)
I have tried adding lines to the bash script replacing "conda activate" with "source activate", I have tried adding echo ". /home/<user>/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc to the bash script, replacing "conda" with /home/barrat/anaconda3/bin/conda, and even adding whoami to the bash script that runs at startup to make sure that i haven't magically become root by chance... none of this has worked. I would really appreciate any help. it's 3 AM and i'm a bit desperate.
You might have solved the issue yet, but for future viewers, this worked for me:
if [ -f "/path/to/anaconda3/etc/profile.d/conda.sh" ]; then
. "/path/to/anaconda3/etc/profile.d/conda.sh"
CONDA_CHANGEPS1=false conda activate myenv
fi
Add this instead of conda activate myenv.
Well as you are trying to activate an environment to start your scripts, it may also be possible for you to make a startup script itself to do the desired task by using subprocess module from python.
Try making a demo.py script like :
import os
import system
import subprocess
import x
subprocess.run(["command name", "value"]) #for all scripts you want to execute
and then you can put this python script to run at startup.
You can start quite a few amount of operations without noticable speed changes to your system and always can monitor it easily by starting the processes one after the other using time.sleep() in between two calls.

Run virtualenvwrapper commands from python script

When I'm trying to create a new Python 3 virtual environment by using mkvirtualenv (virtualenvwrapper command) and os.system like this
import os
os.system('mkvirtualenv foo')
nothing happens.
os.system("mate-terminal -e 'workon foo'")
doesn't work either.
The point is to quickly create a new virtual env and work on it later for each project (it's an automation script). virtualenvwrapper is the most convenient option.
The mkvirtualenv and workon commands are shell functions, not executables in your PATH[0].
To make them available in the shell you execute them in, you need to source the virtualenvwrapper.sh shell script defining them. You might be better off calling virtualenv /path/to/foo directly.
How to activate that virtualenv is another story, though, and will depend on the context you want to use it in. If you activate it in a subprocess, each process using it will have to be run in or under that child.
Hth,
dtk
PS In addition, you might look into the subprocess module (or even the third-party sh) for calling external programs. Happy coding :)
[0]: See $ which workon in a terminal vs $ which bash
The following codes in the bash shell script
env_name="<your env name>"
echo "Create virtual environment"
source `which virtualenvwrapper.sh`
mkvirtualenv $env_name -p python<$version>
source $HOME/.virtualenvs/$env_name/bin/activate
workon $env_name
then run bash script (for example: test.sh) from terminal source test.sh

Is there a single line way to run a command in a Python venv?

I have a command that only runs correctly inside a Python virtual environment I've configured (as intended). I know that I can run the command as
$ cmd args
once I've activated the venv. But (due to the constraints of the tool I'm using) I need to activate run (and deactivate?) in one line: something equivalent to running
$ activate_somehow cmd args
outside the command line.
Is there a way to do this?
You can generally run something in a virtual environment simply by using a fully qualified path to the script. For example, if I have:
virtualenv .venv
Then I can install something into that virtual environment without activating it by running:
.venv/bin/pip install foo
This should be true for anything installed using standard Python mechanisms.
After looking into the generated bin/activate script, it seems like the only thing relevant to python is the VIRTUAL_ENV variable, so this should be enough to get going:
$ env VIRTUAL_ENV=path/to/venv python ...
Note that the python executable in the bin directory of target environment is just a symlink to globally installed interpreter, which does nothing other that setting process executable path. Assuming the program does not make use of it, utilizing the main binary itself seems harmless. In case you have installed a package which in turn installs some executables, just specify the absolute path:
$ env VIRTUAL_ENV=path/to/venv path/to/venv/bin/executable
You can create a simple wrapper script which runs activate, executes your command, and then deactivates simply by exiting the script in which your environment was activated.
#!/bin/sh
. ${venv-./env}/bin/activate
"$#"
This lets you set the environment variable venv to the path of the environment you want to use, or else uses ./env if it is unset. Perhaps a better design would be to pass the env as the first parameter:
#!/bin/sh
. "$1"/bin/activate
shift
"$#"
Either way, save this somewhere in your PATH ($HOME/bin is a common choice for your private scripts) and give it executable permission.
I found venv-run which should do what you ask:
pip install venv-run
venv-run cmd args
Larsk's answer is probably cleaner, but this is another possible way.
Assuming you use UNIX and your user is user and you have a virtual environment in home (any) directory, ie /home/user/venv, you can make a script like:
#!/bin/sh
export VIRTUAL_ENV=/home/user/venv
export PATH=/home/user/venv/bin:$PATH
python3 "$#"
We can make this script executable (eg call it venv-python3 and do chmod +x venv-python3) and call it as such, or put it some place discoverable in PATH - let's say alongside python. Assuming you have sudo rights:
sudo cp venv-python3 /usr/bin/venv-python3
Then we can call that instead of the python callable. Since the variables are set within the script, explicit call on deactivate is not necessary at exit.
Example:
user#machine ~ % venv-python3 --help
This works for at least for virtualenv version 20.0.17 but if adopted, you should be keeping an eye on what variables bin/activate sets, if this ever changes.
Yes, you can execute the python file using a virtual environment in a single line of command on windows.
venv\Scripts\activate&&python fall_detector.py
I installed pgadmin4 in my home directory in a virtual environment called "pgadmin4".
I use fish shell and it runs perfectly fine with:
~/pgadmin4/bin/python3 ~/pgadmin4/lib/python3.10/site-packages/pgadmin4/pgAdmin4.py
Just in case this helps somebody.

os.getenv("xxx") shows different result while using sudo or not to run python3

It happened to me when I try to use os.getenv.
I have edited the "\etc\profile" file to add an env variable and ran the source command.
And I wrote some codes to test it.It worked well and I can get the env variable I set when I run python just using python3 command in the command line.
But I find it doesn't work when I using sudo python3.The os.getenv("xxx") returns None.
That's the question.Why it doesn't work just because of using sudo.
sudo does not keep the user's environment variables. Maybe this can help:
How to keep Environment Variables when Using SUDO

python how to run python without specifying the interpreter

when you run a python script, you have to do
python3 filename
Is there something you can write in the python file to make it so that you dont have to say python3 before running it. I tried the #!/ line, but when I do:
./filename
it says permission denied. Is specifying the interpreter name when running the program mandatory?
At the top of your python file, you'll want to add the path to the Python3 binary. This is commonly referred to as a "hashbang" or "shebang". It tells your shell how to interpret or run your file (without it, if you tried ./<python-file>, it would try to interpret it as bash.
#!/path/to/python3
On my computer, it's
#!/usr/bin/python3
To determine the path where your python3 binary (or link) is located, run
$ which python3
Alternatively, it's better to use env, as it will ensure the interpreter used is the first one on your environment's $PATH.
#!/usr/bin/env python3
Note, you'll need to run
$ chmod a+x <python-file>
to change the mode to make it executable. The a tells it to make it executable for all (user, group, and others), so if you do not want this, you can leave it out (as in, chmod +x <python-file>).
To not have to run ./ before the executable, you'll want to set your PATH as
export PATH=$PATH:.
in your .bashrc or similar *rc file for your shell. (export makes the variable available to sub-processes.) Then you'll want to run
$ source ~/.bashrc
I'm guessing you are on a linux or unix bases operating system. Yes there is something you can do. Hopefully you are using the import os and import sys library for any interaction with terminal. Next you have to do a chmod command on the file to make it executable
The command would be
chmod +x [python_file.py]
or usually (if not root)
sudo chmod +x [python_file.py]

Categories

Resources