Python cannot access my usb device - insufficient permission - python-3.x

Python script cannot send print to my usb Epson receipt printer
I am new to Ubuntu. I am trying to send some text to a thermal label printer connected to my computer via usb. Upon executing the code in Visual Studio Code by pressing the F5 key, I get the below error
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/usb/backend/libusb1.py", line 595, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
However, if I execute the same code with:
sudo python3 -m test5.py
then, it prints out all right. I have done many google searches, and it seems that this is a problem related to my rule settings, so I made the following rule in /etc/udev/rules.d/printerRule.rules
SUBSYSTEM=="usb", ATTRS{idVendor}=="1208", ATTRS{idProduct}=="3605", MODE="0666", OWNER="hdadmin"
Still the problem persists.
The reason why I need to solve this problem is, I later need to compile the Python script using pyinstaller. If I compile as it is right now with
pyinstaller --onefile test5.py
The compile process will be fine, but I will still not be allowed to use the usb device when running the compiled file. Below is the full error report
(env) (base) hdadmin#HiddenDimsum:~/Documents/Python/POS$ cd /home/hdadmin/Documents/Python/POS ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /home/hdadmin/Documents/Python/POS/env/bin/python3.6 /home/hdadmin/.vscode/extensions/ms-python.python-2019.9.34911/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 42379 /home/hdadmin/Documents/Python/POS/test4.py
Traceback (most recent call last):
File "/home/hdadmin/.vscode/extensions/ms-python.python-2019.9.34911/pythonFiles/ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "/home/hdadmin/.vscode/extensions/ms-python.python-2019.9.34911/pythonFiles/lib/python/ptvsd/__main__.py", line 432, in main
run()
File "/home/hdadmin/.vscode/extensions/ms-python.python-2019.9.34911/pythonFiles/lib/python/ptvsd/__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "/usr/lib/python3.6/runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "/usr/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/hdadmin/Documents/Python/POS/test4.py", line 2, in <module>
printer = Usb(1208, 3605)
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/escpos/printer.py", line 51, in __init__
self.open()
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/escpos/printer.py", line 62, in open
check_driver = self.device.is_kernel_driver_active(0)
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/usb/core.py", line 1061, in is_kernel_driver_active
self._ctx.managed_open()
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/usb/core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/usb/core.py", line 120, in managed_open
self.handle = self.backend.open_device(self.dev)
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/usb/backend/libusb1.py", line 786, in open_device
return _DeviceHandle(dev)
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/usb/backend/libusb1.py", line 643, in __init__
_check(_lib.libusb_open(self.devid, byref(self.handle)))
File "/home/hdadmin/Documents/Python/POS/env/lib/python3.6/site-packages/usb/backend/libusb1.py", line 595, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
Since I am new to Linux and Ubuntu, I need help and advices so that I can get permission to the usb devices connected to my computer. The expected outcome is, I should be able to run the test5.py by without the need for
sudo python3 -m test5.py

Thanks to gst. You are fully right. My mistake is during my .rule creation
SUBSYSTEM=="usb", ATTRS{idVendor}=="1208", ATTRS{idProduct}=="3605", MODE="0666", OWNER="hdadmin"
Instead it should look as below, where GROUP="users". I misread the "users" and substituted it with the user name. I changed my rule to as below:
SUBSYSTEMS=="usb", ATTRS{idVendor}=="1fc9", ATTRS{idProduct}=="2016", GROUP="users", MODE="0666"
and then in termial write
$ sudo udevadm control --reload
and restarted the computer.

Related

Could not open extension control file citext.control

Environment(s)
Ubuntu 20.04 & Debian 10 with Python 3.8 or 3.7, respectively.
Postgresql versions 11, 12, and 14 have been tried.
Psycopg2-binary 2.8.6
Overview
I'm attempting to install a Django project, and I'm getting this error:
psycopg2.errors.UndefinedFile: could not open extension control file "/usr/share/pgsql/extension/citext.control": No such file or directory
The psycopg devs informed me this is likely an issue with the postgresql-contrib libraries. Similarly, others have been able to fix this error by installing postgresql-contrib, however, this does not work for me. I've also tried installing postgresql-12.
I can see that citext.control is available in /usr/share/postgresql/12/extension/citext.control, so I tried ln -s /usr/share/postgresql/12 /usr/share/pgsql with no effect.
I also ran CREATE EXTENSION citext; in Postgres, also without effect.
Any support with this would be greatly appreciated, as I was hoping to have this project live already!
Thanks so much.
Trace
Running migrations:
Applying core.0043_install_ci_extension_pg...Traceback (most recent call last):
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedFile: could not open extension control file "/usr/share/pgsql/extension/citext.control": No such file or directory
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 16, in <module>
execute_from_command_line(sys.argv)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/user/janeway/src/utils/management/commands/install_janeway.py", line 58, in handle
call_command('migrate')
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/core/management/__init__.py", line 131, in call_command
return command.execute(*args, **defaults)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 202, in handle
post_migrate_state = executor.migrate(
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/contrib/postgres/operations.py", line 17, in database_forwards
schema_editor.execute("CREATE EXTENSION IF NOT EXISTS %s" % schema_editor.quote_name(self.name))
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 136, in execute
cursor.execute(sql, params)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/user/venvs/janeway/lib/python3.8/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.OperationalError: could not open extension control file "/usr/share/pgsql/extension/citext.control": No such file or directory
Have you tried installing python3-psycopg2 from the packaging system instead?
My Ubuntu 20 setup has this installed, and postgresql connections work with django.
ii python3-psycopg2 2.8.4-2 amd64 Python 3 module for PostgreSQL
I had installed postgresql-contrib on the local django server rather than the remote DB server. Installing on the same server as Postgresql resolved the issue.

az login command failure

Trying to set up Azure CLI on my Kali Linux. This is the version of kali.
kali#kali:~$ uname -a
Linux kali 5.5.0-kali2-amd64 #1 SMP Debian
5.5.17-1kali1 (2020-04-21) x86_64 GNU/Linux
After installation, when I run az login, it takes me to the browser where I enter required credentials that are accepted, and the browser then shows following message.
You have logged into Microsoft Azure!
However, in the shell window, it shows the following error.
kali#kali:~$ az login
You have logged in. Now let us find all the subscriptions to which you have access...
The command failed with an unexpected error. Here is the traceback:
'SubscriptionClient' object has no attribute 'config'
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/knack/cli.py", line 215, in invoke
cmd_result = self.invocation.execute(args)
File "/usr/lib/python3/dist-packages/azure/cli/core/commands/_init_.py", line 654, in execute
raise ex
File "/usr/lib/python3/dist-packages/azure/cli/core/commands/_init_.py", line 718, in _run_jobs_serially
results.append(self._run_job(expanded_arg, cmd_copy))
File "/usr/lib/python3/dist-packages/azure/cli/core/commands/_init_.py", line 711, in _run_job
six.reraise(*sys.exc_info())
File "/usr/lib/python3/dist-packages/six.py", line 703, in reraise
raise value
File "/usr/lib/python3/dist-packages/azure/cli/core/commands/_init_.py", line 688, in _run_job
result = cmd_copy(params)
File "/usr/lib/python3/dist-packages/azure/cli/core/commands/_init.py", line 325, in __call_
return self.handler(*args, **kwargs)
File "/usr/lib/python3/dist-packages/azure/cli/core/_init_.py", line 782, in default_command_handler
return op(**command_args)
File "/usr/lib/python3/dist-packages/azure/cli/command_modules/profile/custom.py", line 152, in login
subscriptions = profile.find_subscriptions_on_login(
File "/usr/lib/python3/dist-packages/azure/cli/core/_profile.py", line 195, in find_subscriptions_on_login
subscriptions = subscription_finder.find_through_authorization_code_flow(
File "/usr/lib/python3/dist-packages/azure/cli/core/_profile.py", line 849, in find_through_authorization_code_flow
result = self._find_using_common_tenant(token_entry[_ACCESS_TOKEN], resource)
File "/usr/lib/python3/dist-packages/azure/cli/core/_profile.py", line 893, in _find_using_common_tenant
client = self._arm_client_factory(token_credential)
File "/usr/lib/python3/dist-packages/azure/cli/core/_profile.py", line 812, in create_arm_client_factory
configure_common_settings(cli_ctx, client)
File "/usr/lib/python3/dist-packages/azure/cli/core/commands/client_factory.py", line 79, in configure_common_settings
client.config.enable_http_logger = True
AttributeError: 'SubscriptionClient' object has no attribute 'config'
To open an issue, please run: 'az feedback'
What does this error mean and how can this be addressed?
honestly this looks like a version mismatch on the libraries you are using? I'd probably remove az cli completely and remove all the python modules that are related to azure and reinstall it, alternatively, just use a docker image with az cli provided by microsoft:
docker run -it mcr.microsoft.com/azure-cli

jupyter nbconvert --to slides fails due to access permissions

I'm trying to generate a slideshow for my notebook using nbconvert. Using windows, jupyter and python 3.
https://nbconvert.readthedocs.io/en/latest/usage.html#convert-revealjs
I'm running the command in a command shell in admin mode:
jupyter nbconvert C:\files\notebook.ipynb --to slides --post serve
The files\ directory contains a directory called reveal.js.
The following error is raised,
[NbConvertApp] Converting notebook C:\files\notebook.ipynb to slides
[NbConvertApp] Writing 379320 bytes to C:\files\notebook.slides.html
[NbConvertApp] Serving local reveal.js
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\Scripts\jupyter-nbconvert-script.py", line 5, in <module>
main()
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\jupyter_core\application.py", line 267, in launch_instance
return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\nbconvert\nbconvertapp.py", line 293, in start
self.convert_notebooks()
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\nbconvert\nbconvertapp.py", line 457, in convert_notebooks
self.convert_single_notebook(notebook_filename)
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\nbconvert\nbconvertapp.py", line 430, in convert_single_notebook
self.postprocess_single_notebook(write_results)
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\nbconvert\nbconvertapp.py", line 402, in postprocess_single_notebook
self.postprocessor(write_results)
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\nbconvert\postprocessors\base.py", line 28, in __call__
self.postprocess(input)
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\nbconvert\postprocessors\serve.py", line 87, in postprocess
http_server.listen(self.port, address=self.ip)
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\tornado\tcpserver.py", line 126, in listen
sockets = bind_sockets(port, address=address)
File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\tornado\netutil.py", line 194, in bind_sockets
sock.bind(sockaddr)
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
Which indicates that the port is already in use locally- I believe it defaults to 8000. Is there a way to specify the port when running the nbcovert command? E.g. not 8000.
NB The file notebook.slides.html is being created, and I can manually serve it up with http.server on a different port.
You can indeed change the port. Modify the command as follows:
jupyter nbconvert C:\files\notebook.ipynb --to slides --ServePostProcessor.port=8910 --post serve

Apache Storm nimbus start fails when run using python

I am trying to install and run apache storm(version 1.0.2) on amazon linux instance.There is no problem in starting storm nimbus when I am root user. In other words, if is use the following commands
sudo su -
python /opt/apache-storm-1.0.2/bin/storm.py nimbus
it is working fine.
However if am an ec2-user and tries to execute the command as
sudo python /opt/apache-storm-1.0.2/bin/storm.py nimbus
it is giving the following error
Traceback (most recent call last):
File "./storm.py", line 769, in <module>
main()
File "./storm.py", line 766, in main
(COMMANDS.get(COMMAND, unknown_command))(*ARGS)
File "./storm.py", line 529, in nimbus
jvmopts = parse_args(confvalue("nimbus.childopts", cppaths)) + [
File "./storm.py", line 145, in confvalue
p = sub.Popen(command, stdout=sub.PIPE)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Is there anyway to run the command without switching as root user

urllib.urlopen() gives socket error: Name or service not known on python2.7

I am just starting to use and learn Python, so this may seem vary naive to ask.
On my Linux system if i try to get a webpage using urllib.urlopen() I get an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 207, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 344, in open_http
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 776, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 757, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known
>>>
If I try to do the same in the Python 2.7 installed in my Windows 7 system, it works fine.
Since i am a novice, its difficult for me to diagnose the problem. I tried to research it but still haven't got any answers.
So my questions are:
What is different in the windows system that urlopen() works there but not on Linux.
What needs to be done to ensure that urlopen() works on the Linux system. Its necessary for me that it works since the the program I am developing has some bash command calls and the program depends extensively on the proper working of urllib.

Resources