Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'elevate' (Linux) #7

Closed
dignityinside opened this issue Jun 15, 2019 · 8 comments
Closed

ModuleNotFoundError: No module named 'elevate' (Linux) #7

dignityinside opened this issue Jun 15, 2019 · 8 comments

Comments

@dignityinside
Copy link

  1. I run demo script
  2. Get the screen where i enter my root password
  3. Then i get this error:

`/usr/bin/python3.6 /home/dignity/Projects/HostEditor/main.py
Traceback (most recent call last):
File "/home/dignity/Projects/HostEditor/main.py", line 8, in
from elevate import elevate
ModuleNotFoundError: No module named 'elevate'

Process finished with exit code 1`

Python version:
Python 3.6.7

OS:
Linux Mint 19.1 (Ubuntu 18.04 based, 64 Bit)

How i installed the package:
pip3 install --user elevate

My Example:
`import os
from elevate import elevate

def is_root():
return os.getuid() == 0

print("before ", is_root())
elevate()
print("after ", is_root())`

@dignityinside dignityinside changed the title ModuleNotFoundError: No module named 'elevate' ModuleNotFoundError: No module named 'elevate' (Linux) Jun 15, 2019
@catb0t
Copy link

catb0t commented Jun 16, 2019

I can't reproduce that. Are you sure that pip3 install --user elevate was successful?

@dignityinside
Copy link
Author

dignityinside commented Jun 16, 2019

Yes.

`pip3 install --user elevate

Collecting elevate
Installing collected packages: elevate
Successfully installed elevate-0.1.3`

@barneygale
Copy link
Owner

Could you please run:

python -V
python3 -V
pip -V
pip3 -V

and report back?

@barneygale
Copy link
Owner

I note that Python 3.6 isn't in the Linux Mint 19 package repos. How did you install it?

@dignityinside
Copy link
Author

python -V: Python 2.7.15+
python3 -V: Python 3.6.8
pip -V: not installed
pip3 -V: pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

@catb0t
Copy link

catb0t commented Jun 17, 2019

pip3 install --user installed the package in your home directory, and then the script invoked itself as root, so python looks in /usr/lib/python3/dist-packages/ and /root/.local/lib/python3.6/site-packages/elevate (see SO)

elevate is not available system-wide or for the root user, only for your user.

Due to the unique way that language package managers (pip included) treat library installations, this is (one of) the only cases where such a failure is possible.

If you clone the repository to /home/$USER/git/elevate and run the demo from there, the demo's import elevate will still be resolved to your clone, relative to the script's file location, because it's not a site-package, installed by pacman, apt, or pip3.

When import elevate refers to a site-package (/home/$USER/.local/lib/python3.6/site-packages/elevate), then it's not relative to the file's location, and can't become a user-agnostic absolute path. Instead, it's relative to the user who ran the code, and only your $USER has the package in its site- or dist-packages (installed by pip3); only you can import elevate.

That's a description of the problem; hopefully it's explained well. This is a structural limitation of elevate, but mostly a limitation of pip3 install --user.

I have some ideas about formal solutions or workarounds to elevate, but the simplest workaround for now is

  • Either install the package system-wide with sudo pip3 install -U elevate
  • or clone the repository somewhere in your home or project directory, that's hopefully also root-readable. import elevate will then refer to __init__.py relative to the example code and your code.

@catb0t

This comment has been minimized.

@derVedro
Copy link

derVedro commented Jun 18, 2019

You can see the path where pip3 install --user elevate is installed, at the uninstall prompt:

or you just import elevate and look what elevate.__file__ looks like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants