Andre
Andre

Reputation: 4635

Python Permission Problems Ubuntu

I am trying to run a simple python script

$python Solution.py read_input.txt

Only if I use sudo,

$sudo python Solution.py read_input.txt

I receive the following error message:

Traceback (most recent call last):
  File "Solution1.py", line 2, in <module>
   import numpy as np
ImportError: No module named numpy

This is due to a permission problem.

$which python
/home/andrelandgraf/anaconda3/bin/python

$sudo which python
/usr/bin/python

I want to use my anaconda env to run the file but because of permission reasons I am unable to run python without sudo which will lead to running the system version of python that does not have access to numpy.

I tried chmod and chown to give python the required permissions

$chmod -R 777 .

but whenever I run python without sudo it just prints one empty line and nothing happens.

I also checked my .bashrc file and echoed the $PATH, anaconda3 seems to be installed fine.

$PATH: /home/andrelandgraf/anaconda3/bin:/home/andrelandgraf/bin:/home/andrelandgraf/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

How can I resolve this issue?

Upvotes: 0

Views: 519

Answers (1)

Bakuriu
Bakuriu

Reputation: 101979

sudo does not use the user PATH variable for security reasons. If you want to specify exactly which executable to use you have to use the absolute path:

sudo /home/andrelandgraf/anaconda3/bin/python Solution.py read_input.txt

I must add: you should run programs as root only if it is necessary. In your question you don't say why you need to do this so we cannot help you in fixing the issue with permissions or workarounds.


If you want to know why sudo does not look at what the user has defined as PATH that is simple: if a user downloaded a malware that is called, say, ls then calling sudo ls would run the malware as root which is way worse than running the malware as a anormal user.

so sudo only looks at the system defined PATH variable, hopefully a normal user cannot put such a malware in a system directory and thus sudo ls executes the normal command ls as root instead of the malware.

Upvotes: 1

Related Questions