I'm Mac user.
I want to set PYTHONPATH env for root. so
$ sudo su -
# vi ~/.profile
a开发者_如何学运维nd add to file 'export PYTHONPATH=/mypythonlib'
then
# env
I can see this line
PYTHONPATH=/Users/simpnet2/projects/meiji/src/hershey
but..
when I use sudo command, cannot find that
$ sudo env
.. there's no PYTHONPATH
My program has to run with sudo command and needs PYTHONPATH
.
If you use sh try /etc/profile
, bash try /etc/bashrc
and if you use zsh try /etc/zshenv
.
You can make PYTHONPATH visible to sudo be editing your sudoers file. Notice you should ONLY do this through visudo as explained here.
You should try sudo -i
which will simulate logging in as root
and source the ~root/.profile
.
As of 10.8.5, putting my environment statements in the .profile path in the home of the root user (/var/root) worked. after quitting bash and coming back to the root user prompt with 'su -', I could see my new path, etc. with the 'env' command and my MacPorts installationw orking correctly.
MacBook-Pro:~ root# cat /var/root/.profile export MANPATH=/opt/local/share/man:$MANPATH export PATH=/opt/local/bin:/opt/local/sbin:$PATH MacBook-Pro:~ root# which port /opt/local/bin/port
Well, in other Linux system, it is also right that 'sudo' does not use local environment variable. But you can declare the temporary environment variable along with 'sudo' command.
For example, in your case, you can add 'PYTHONPATH=/mypythonlib' in your command 'sudo env', and the final command is:
sudo PYTHONPATH=/mypythonlib env
You can also read this article: Using sudo. You can see how 'sudo' keep or ignore user-defined environment variables.
In the case of logging in as a normal user and invoking "su - root" I found that Mac OS 10.8.5's bash was ignoring .profile and .bash_profile; I was unable to change root's $PATH by editing those files. What did work was editing /etc/paths. After exiting the root shell and entering again with "su - root" the new path was present.
精彩评论