Python dont find the module when I use it with sudo [duplicate] - python

This question already has an answer here:
Can't import module when using root user [duplicate]
(1 answer)
Closed 2 years ago.
I am using keyboard library that`s need acces to sudo but when I try to run the program with sudo python dont find the others libraries
import pyautogui
import keyboard
if keyboard.is_preseed('w'):
....
So when I use without sudo keyboard dont work and when I use with sudo python dont find the others libraries

'sudo' sets up a new environment because it aims to be safe and because the new user won't have the same view of the world as you have.
You can use the keep-environment parameter to 'sudo' to preserve some of them, but that won't guarantee that your view is the same. It could be a permissions problem, or a relative path problem or a difference in homes, or a user shell-initialization setting in the other user. You can likely fabricate a good environment on the other side of sudo with something like "sudo env PYTHONSOMETHING=$PYTHONSOMETHING programname" .
So, it's complicated. I'd first use "sudo -i" to get an interactive shell and test what it looks like, and find what to change.

Python uses a different package insallation directory for each user by default.
For example, you can find the location of the keyboard package like so:
>>> import keyboard
>>> keyboard.__file__
'/home/user/.local/lib/python3.8/site-packages/keyboard/__init__.py'
As you can see, it is located in /home/user, which means only user is supposed to use it.
In order to install the package for the root user, just run pip with sudo:
sudo pip3 install keyboard

Related

Automatic installation of clickhouse via script, how to answer the question about the default password

I have a question:
I need to install clickhouse-server generic and client. I need to add this to my autoinstall script,
The problem is that during the installation of the package it asks for "Enter the default password", I just need to specify "enter" it blank, how do I auto-reply. one must first expect and include a separate file with the installer.
Maybe someone else has a solution?
I used expect, But it's rather inconvenient as it seems to me, I have to create a separate file so that it reads the execution of my script
Clickhouse respects DEBIAN_FRONTEND
https://www.cyberciti.biz/faq/explain-debian_frontend-apt-get-variable-for-ubuntu-debian/
DEBIAN_FRONTEND=noninteractive apt-get install ....

Python script that leaves an activated virtualenv after it halts [duplicate]

This question already has answers here:
How to source virtualenv activate in a Bash script
(12 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I'm trying to create a utility CLI that creates an empty python project with just a .venv inside.
One of the features I wanted is that, when the CLI ends the project creation, it leaves the bash inside the project folder and with the virtualenv activated. This way:
(base) ubuntu#pc:~/dev/cli$ python cli.py start ./folder
... do it's magic ...
... and ends up like this:
(.venv) ubuntu#pc:~/dev/cli/folder$
Note that now the cwd is inside folder and **(.venv) ** is activated.
I was able to achieve part of my objective (change the folder) by using:
import os
os.chdir("./folder")
os.execl("/bin/bash", "/bin/bash")
But didn't find a way to keep the venv activated after the program is halted.
Any ideas??
bash has the -rcfile option that allows to use a non standard initialization file.
import os
os.chdir("./folder")
os.execl("/bin/bash", "/bin/bash", "--rcfile", ".venv/bin/activate")
That should be enough for the new .venv to be active in the bash shell.

How to run a pip install command from a subproces.run() [duplicate]

This question already has answers here:
Installing python module within code
(12 answers)
Closed 1 year ago.
I see in this article: making and automatic python installer that you can use:
subprocess.run('pip install module_name')
or
subprocess.run('pip install -r requirements.txt')
in this format to install modules either individually or form a file. But when i run this command I get this error:
FileNotFoundError: [Errno 2] No such file or directory:
is there a way to run this like that without having to do this:
subprocess.run(['pip', 'install', 'module_name'])
I suggest you stick to subprocess.run(['pip', 'install', '-r', 'requirements.txt']). To quote the subprocess docs.
Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).
Avoiding the use of shell=True will save you a lot of trouble if you ever want to run the script on a different system. In general, though, I recommend avoiding reliance on a shell. That is, after all, why you are using Python and not, say, Bash.
To make it work on Linux I used the shell=True parameter. On Windows, it worked perfectly, without it.
subprocess.run("python -m pip install -r requirements.txt", shell=True)
Found the answer here.

running as sudo from python for pyshark / tshark

I'm trying to do a live capture with pyshark, but it wants to run tshark using sudo. I'm not sure how to run sudo out of python. The github thread states: "you can create a 'script' that just runs "sudo tshark" and tell pyshark to run that instead of tshark."
Buuuuut I'm not too sure how to do that. I was looking at Using sudo with Python script
but again not sure how to "run that instead of tshark"
Has anyone done this? Can anyone advise?
Bit more info here: If you're an admin user, you don' t need sudo to run "tshark -c 100 -i en0". If you "sudo chmod 777 /dev/bpf*" that works for things like Carnivore in Processing, but does zip all for Pyshark. Trying to edit Startup items to give you read access is moot on OSX because Yosemite tossed it.
Other info: https://apple.stackexchange.com/questions/138694/what-is-access-bpf-group
I'm really starting to think something is just up w/ PyShark itself.
Thanks
Don't use sudo to run Wireshark. Instead, configure your user account to be able to use Wireshark without root access. Detailed instructions are here: https://ask.wireshark.org/questions/7976/wireshark-setup-linux-for-nonroot-user
WELP. turns out it was just because I hadn't used 'en0' Marking this as solved. HA.

Python pip run but dont see anything when running

i use python 2.7.10 and window 7. When i use "pip" to install new package, i don't see anything when it run like this:
install pillow using pip
Can anybody tell me how to fix it?
PIP uses a configuration file called pip.ini, in which you can set default command-line options for PIP. Chances are the --quiet option is turned on by default; this option causes PIP to produce no output unless there's an error, just like in your case.
There are multiple places that it could be:
C:\Users\you\AppData\Roaming\pip\pip.ini
C:\ProgramData\pip\pip.ini
You need to edit your file (if they're both present, edit both) to remove the lines that look like one of the following:
quiet = true / on / yes
q = true / on / yes
Save the file(s) and you should then see output from PIP the next time you run it.
If there's no pip.ini
If you can't find a pip.ini file anywhere, create one in:
C:\Users\you\AppData\Roaming\pip\pip.ini
Open it using a text editor, then put this inside it:
[global]
quiet = false
Demonstration
Here's a GIF demonstrating the difference between --quiet and no --quiet:

Categories