Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python can't find module when started with sudo

I've got a script that uses the Google Assistant Library and has to import some modules from there. I figured out this only works in a Python Virtual Environment, which is really strange. In the same folder I've got a script which uses the GPIO pins and has to use root. They interact with each other, so when I start the GPIO script, the Assistant script is also started. But for some reason the modules in there can't import when the script is started with root. Does anybody know something about this?

like image 488
Jan Avatar asked Jun 11 '17 12:06

Jan


People also ask

Why is Python not finding my module?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

How do I find where a Python module is located?

You can manually go and check the PYTHONPATH variable contents to find the directories from where these built in modules are being imported. Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to know the location of built in modules.

Is __ init __ py a module?

The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.

How do I run a sudo in Python?

From the terminal instead of doing python yourProgram.py , do sudo python yourProgram.py . It will ask for your password so type it and it should run.


3 Answers

Normally you can active a virtual env and use the interpreter inside the env to run your script. But it is not necessary.

Suppose you have a virtual env under the path /path-to-env/env the script you want to run example.py is under the path /path-to-script/example.py

you can already run this example.py like

sudo /path-to-env/env/bin/python /path-to-script/example.py
like image 112
milo Avatar answered Oct 22 '22 04:10

milo


not 100% sure but have you tried:

sudo -E python myScriptName.py

As mentioned here

like image 23
Giorgos Xou Avatar answered Oct 22 '22 04:10

Giorgos Xou


Try to install the module using sudo.

I had the same problem with the module 'reportlab' from python. I realized that I had installed pip (the installer manager for reportlab) without sudo command.

The problem is that the package (pip and reportlab) has been installed as user and not as root, so when you try to use sudo, it does not recognize the system path to reportlab because you never installed in the first place, only for the user!

I recommend install pip and module with sudo always:

For python 2:

$ sudo add-apt-repository universe
$ sudo apt update
$ sudo curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
$ sudo python2 get-pip.py
$ sudo pip install google-assistant-library

For python 3 (from Docs Google assistant library):

$ sudo apt-get update
$ sudo apt-get install python3-dev python3-venv
$ sudo python3 -m venv env
$ sudo env/bin/python -m pip install --upgrade pip setuptools
$ sudo source env/bin/activate
$ sudo python -m pip install --upgrade google-assistant-library

Hope this helps! Regards!

like image 32
Stéfano Oliveira Avatar answered Oct 22 '22 03:10

Stéfano Oliveira