-1

I was having trouble about python unicode and so I reinstalled python on /usr/local/bin/python with option "--enable-unicode=ucs4". I added to ~/.bashrc all the paths to python modules and when I run as common user I'm able to import modules, but when I'm as sudo I can't.

iury@buzios:~$ /usr/local/bin/python
Python 2.7.6 (default, Aug 20 2015, 11:57:25) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>  
iury@buzios:~$ sudo /usr/local/bin/python
Python 2.7.6 (default, Aug 20 2015, 11:57:25) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> 
iury simoes-sousa
  • 1,440
  • 3
  • 20
  • 37
  • Before anyone answers: do you have a *strong* reason to run Python as root? Otherwise, reduce its privileges and install your required packages in a virtualenv. – Makoto Aug 20 '15 at 17:13
  • It's used when I'm trying to install a python module. – iury simoes-sousa Aug 20 '15 at 19:09
  • Are you installing the module for local development? Is there something preventing you from migrating towards a virtualenv instead? – Makoto Aug 20 '15 at 19:54
  • I don't know how to use virtualenv to avoid issues against environment variables. It creates something like virtual computer? is this like miniconda? Sorry, I'm to noob.. :( – iury simoes-sousa Aug 20 '15 at 20:00

1 Answers1

1

As their name indicates, Environmental Variables are assigned to your own user. That means that if you are running program as root (using sudo), they won't be "assigned" to the program because it is running in the environment of the root user. A work around is to set the environment variables after the sudo command like this: sudo env PATH=$PATH VAR1=SOME_VALUE VAR2=SOME_VALUE

As the answer in this question indicates, you can add this export in your ./bashrc as a workaround:

alias sudo='sudo env PYTHONPATH=[PATH] PYTHON=[OTHERPATH] ... ./thescript.py

Also, as mentioned in the comments, make sure you need to run python as sudo, as it is not recommended when not REALLY needed.

Community
  • 1
  • 1
matan7890
  • 521
  • 3
  • 22
  • I'll try it.. This command is on a setup.py of python module. – iury simoes-sousa Aug 20 '15 at 19:11
  • Sorry about me. I'm not sure what I have to do. The problem is that I did run the './runsetup.py --sudo' of package before and I got no problem importing. Why does it change? I have a lot of PYTHONPATHs of my user that the installation could use. If I use '--sudo' I got this problem and when I run just './runsetup.py' I got no permission to create the '.so' file. – iury simoes-sousa Aug 20 '15 at 19:52
  • Again, environmental variables like PYTHONPATH are assigned to all programs that run on the same user that exported the variable. When you run a program using sudo, you run the program as the user "root" and not your own user. That means that if in your own user in the computer the variable PYTHONPATH equaled `/user/env/python` (for example), when you run a program using sudo, it won't be available. YOU WILL HAVE TO EXPORT THIS VARIABLE IN THE ROOT ENVIRONMENT. This is why you "export" them (set them) when you are running programs using sudo. Is it clear now? – matan7890 Aug 20 '15 at 21:36
  • Also, please remember to mark this answer as the right one if it works for you. – matan7890 Aug 20 '15 at 21:40