I am using Fabric and would like to use fexpect. I have the following Python script:
from ilogue.fexpect import expect, expecting, run
(...)
def install_postgresql(profile):
print("!!! Installing PostgreSQL...")
print(' -> Doing pre-cleanup...')
# Remove PostgreSQL if it exists
prompts = []
prompts += expect('Do you want to continue [Y/n]? ', 'Y')
with settings(warn_only=True):
with expecting(prompts):
run('sudo apt-get purge postgresql')
print(' -> Doing actual installation...')
# Install PostgreSQL
prompts = []
prompts += expect('Do you want to continue [Y/n]? ', 'Y')
with expecting(prompts):
run('sudo apt-get install postgresql')
# In some cases PostgreSQL has issues with Ubuntu's default kernel params
# that prevent PostgreSQL to start automatically, so we try to start it
# TODO: Fix it
with settings(warn_only=True):
run('sudo service postgresql start')
When executing I get the following error:
[xxx.xxx.xxx.xxx] out: Traceback (most recent call last):
[xxx.xxx.xxx.xxx] out: File "/tmp/fexpect_MbW3QP6Zpy5KBjBGQcaYxi", line 4, in <module>
[xxx.xxx.xxx.xxx] out: import pexpect
[xxx.xxx.xxx.xxx] out: ImportError: No module named pexpect
I am using virtualenv and pexpect is actually installed:
(venv)PALM00545424A:woopup i841712$ pip install pexpect
Requirement already satisfied (use --upgrade to upgrade): pexpect in ./venv/lib/python2.7/site-packages
Found the solution.
pexpect was not part of the remote machine's Python installation.
I simply executed
sudo -E pip install pexpect
on the remote machine.
In fact if your script uses fexcept, the command you need to run is actually:
sudo -E pip install fexpect
Not a direct answer to your question, but tools like chef, puppet or salt are more suitable for installing system packages.
I got the same error when using pexpect lib to interact with gatttool. I used Pycharm to remote debug code on Raspberry pi. Here is the command processed by Pycharm and the error output
sudo+ssh://pi3#192.168.x.x:22/usr/bin/python3 -u /tmp/pycharm_project_55/Rasp_Pi/BluetoothBLEComm.py
Traceback (most recent call last):
File "/tmp/pycharm_project_55/Rasp_Pi/BluetoothBLEComm.py", line 33, in <module>
import pexpect
ModuleNotFoundError: No module named 'pexpect'
After spending few hours, I identified the issue is with the option I checked while configuring the Remote Python Interpreter in Pycharm. It is the option that executes code with root privileges via sudo.
**sudo**+ssh://pi3#192.168.x.x:22/usr/bin/python3...
The pexpect package was only installed for my local pi3 user. So to solve the issue either I had to install pexpect using sudo or uncheck the option that executes the code with root privileges.
Related
I want to use the Python package keyring to avoid exposing passwords in my Python scripts. According to what I've read, all you need to do is pip install keyring and then start storing and retrieving credentials. I've done this on both my Mac laptop (Big Sur) and a CentOS 7 box. On my Mac, pip list shows that version 23.1.0 of keyring is installed, and the command line utility is available. However, when I try to set a password using the command line or in python using keyring.set_password(...) I get the following error:
File "/lib/python3.7/site-packages/keyring/backends/macOS/init.py", line 38, in set_password
api.set_generic_password(self.keychain, service, username, password)
NameError: name 'api' is not defined
Line 12 of the init.py is a simple import of the api module:
from . import api
and then line 38, where the error is reported, tries to set the password:
api.set_generic_password(self.keychain, service, username, password)
Why am I getting this error? I read somewhere that you shouldn't install keyring in a venv with pip but I'm not certain why not. Any help is appreciated.
Error was introduced with version keyring==23.1.0 (Aug 15, 2021)
Once I downgraded to keyring==23.0.1 (March 25, 2021), the problem went away.
Try installing the six package by typing this in the command prompt/terminal:
pip install six
Somehow, the keyring package isn't installed properly on MacOS. See here.
Run this command:
$ /usr/local/lib/python3.7 -c "import keyring.backends._OS_X_API"
That should produce the underlying exception that's causing the module to be missing.
EDIT:
Try adding this
if sys.platform == 'darwin': # It's a solution for Mac
import keyring.backends.OS_X
keyring.set_keyring(keyring.backends.OS_X.Keyring())
else: # It's a solution for Windows
import keyring.backends.Windows
keyring.set_keyring(keyring.backends.Windows.WinVaultKeyring())
I am using Ubuntu. I want to spam the bus and view the packets using Wireshark. I tried to run the Python code shown below, but it threw the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'can'
My code:
import time, can
bustype = 'socketcan'
channel = 'vcan0'
def producer(id):
# :param id: Spam the bus with messages including the data id.
bus = can.interface.Bus(channel=channel, bustype=bustype)
for i in range(10):
msg = can.Message(arbitration_id=0xc0ffee, data=[id, i, 0, 1, 3, 1, 4, 1], extended_id=False)
bus.send(msg)
# Issue #3: Need to keep running to ensure the writing threads stay alive.
time.sleep(1)
producer(10)
Ubuntu 18.04 and later
In Ubuntu 18.04 and later python-can and python3-can are provided by the default Ubuntu repositories. Open the terminal and type:
sudo apt update
sudo apt install python3-can # for Python 3.x
or
sudo apt update
sudo apt install python python-can # for Python 2.x
To install CANard (Library for interacting with Controller Area Network (CAN)) you must use pip.
python3 -m pip install --user CANard # for Python 3.x
or
python -m pip install --user CANard # for Python 2.x
Whenever you get the no module named '<module_name>' error in Python, it means that python can not find the module. Likely because module is missing.
You can install python modules using pip.
If you don't have the pip tool then you can install it using sudo apt install python-pip on Debian based operating systems.
In your case you need python-can module which can be install by using
pip install python-can
You need to install external third-party package python-can
You can do so using pip. Follow the instructions in the provided link
I am using windows 8 and python 3.6.1 I've done the following command in my cmd:
pip install cryptoshop
However, when I run the following python code:
from cryptoshop import encryptfile
from cryptoshop import decryptfile
result1 = encryptfile(filename="test", passphrase="mypassphrase", algo="srp")
print(result1)
result2 = decryptfile(filename="test.cryptoshop", passphrase="mypassphrase")
print(result2)
I get the following error:
Traceback (most recent call last):
File "C:/Users/Owner/Desktop/test.py", line 1, in
from cryptoshop import encryptfile
File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cryptoshop__init__.py", line 26, in
from cryptoshop.cryptoshop import encryptfile
File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cryptoshop\cryptoshop.py", line 56, in
from ._cascade_engine import encry_decry_cascade
File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cryptoshop_cascade_engine.py", line 27, in
from ._nonce_engine import generate_nonce_timestamp
File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cryptoshop_nonce_engine.py", line 39, in
import botan
ModuleNotFoundError: No module named 'botan'
Now, I obviously know that you must install botan into python in order to use it. However, this is where I am running into an issue. I've downloaded Botan from this link as instructed:
https://github.com/randombit/botan
And then I've followed these instructions in an attempt to install Botan:
./configure.py [--prefix=/some/directory]
make
make install
However, when I type make into the command line I get an error saying there is no such command. And then when I go to run my above Python code I still get the no module Botan error. So obviously I am doing something run. How can I properly install Botan into my Python 3.6 directories so that I can use cryptoshop.
I've also attempted to do pip install Botan, as that is how I've installed so many other python libraries but that has been unsuccessful as well.
make is a linux command
According to the botan website you can use nmake as a replacement on windows ( http://wiki.c2.com/?UsingNmake ) :
On Windows
You need to have a copy of Python installed, and have both Python and
your chosen compiler in your path. Open a command shell (or the SDK
shell), and run:
$ python configure.py --cc=msvc (or --cc=gcc for MinGW) [--cpu=CPU]
$ nmake
$ botan-test.exe
$ nmake install
Botan supports the nmake replacement Jom which enables you to run
multiple build jobs in parallel.
source : https://botan.randombit.net/manual/building.html
For completeness, here's how I made it work on a Mac
Assuming you have brew installed.
brew install botan
You may need to install other functionality first:
brew install gmp
brew install mpfr
brew install mpc
Find out where botan got installed with brew info botan.
My location is /usr/local/Cellar/botan/2.6.0
In that folder, you'll find lib/python2.7/site-packages, copy the contents of this folder into your Python's installation site-packages folder.
Note 1: At the time of this writing, only python 2.7 seems to be supported, but I'm using python 3.6 and everything seems to be working.
Note 2: If the file is called botan2.py, you may need to rename it to botan.py in your python's site-packages folder.
Im trying to run a package wal-e which I have installed as user root with sudo python3 -m pip install wal-e[aws,azure,google,swift].
I can run this command perfectly as user root using envdir /etc/wal-e.d/env wal-e backup-fetch /var/lib/postgresql/9.6/main LATEST.
However, when I sudo su - postgres and then run envdir /etc/wal-e.d/env wal-e backup-fetch /var/lib/postgresql/9.6/main LATEST, I get the error
Traceback (most recent call last):
File "/usr/local/bin/wal-e", line 7, in <module>
from wal_e.cmd import main
ImportError: No module named 'wal_e.cmd'
I gave user postgres full sudo permissions with usermod -aG sudo postgres. Also the wal-e package is installed in the same location.
When I run ls -la I get
-rwxr-xr-x 1 root root 211 Sep 20 14:24 /usr/local/bin/wal-e
Im also on Ubuntu 16.04.3
How can I run the command just like the root user?
I had to run a strict setup process for wal-e in order for the package to function properly.
Virtually what it boiled down to was installing all necessary dependencies on the machine that I was working with before installing and creating the user postgres. If the user was created before all the dependencies were installed, I got permissions errors.
Yesterday I finished the tutorial for Django framework.
On this tutorial I've created simple application and now I'd like to move my app to remote internet server. I have such server , I'm connected to SSH by putty and when I write python, I see: Python 2.7.12 (default, Nov 19)
But if I try to put this commend: python setup.py install
I get:
Traceback (most recent call last):
File "setup.py", line 5, in <module>
from setuptools import find_packages, setup
ImportError: No module named setuptools
and it's strange because after this:
>>> import django
>>> django.VERSION
I see the version: (1, 10, 5, u'final', 0)
And I copied a folder with my app by FTP
So, can you tell me, step by step, how can I run my application?
Thanks for your answers. I've tried this code:
sudo apt-get install python-setuptools
and i received this message:
sudo: effective uid is not 0, is /usr/bin/sudo on a file system with
the 'nosuid' option set or an NFS file system without root privileges?
Any Ideas ?
If you're on a Debian based system, try running:
sudo apt-get install python-setuptools
to install Python setuptools package
If you're not on a Debian based system, look at the package installing guide here.
You're missing setuptools. Install it and the error will be gone.