Error with module Socket on Python3.6 - bluetooth

First the context : I Want to send some data with a Bluetooth connection.
And to do that I want to use Python 3.6 because it's with this programming language that I coded the rest of the algorithm. I use it on a Raspbian Jessie lite for raspberry pi based on Debian Jessie.
The problem is that when I use this code :
import socket
hostMACAddress = 'xx:xx:xx:xx:xx:xx' # The MAC address of a Bluetooth adapter on the server
port = 3
backlog = 1
size = 1024
s=socket.socket(socket.AF_BLUETOOTH,socket.SOCK_STREAM,socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)
try:
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
print("data")
client.send(data)
except:
print("Closing socket")
client.close()
s.close()
This error look like :
Traceback (most recent call last):
File "TestSocket.py", line 5, in <module>
s =socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'
I used it from a site which indicate that it's functionnal for Python 3.3 and above.
It's complicate because without this attribute I don't know how use socket module to establish bluetooth connection and send data correctly.
I am not an expert in Python so any help is welcome or with Debian distribution.
Thinx for you time and sorry for any syntax error or misspellings english is not my mother tongue.

Raspbian comes with two Python versions installed, 2.7 & 3.4.
Python 2.7 is the default so typing python script.py at the command prompt will run the script against Python 2.7
If you use python3 script.py you should find your script works
Also please note that the latest Python version available for Raspbian Jessie is 3.4

Related

How can I resolve python requests package import error?

I am new to python and am having trouble getting the python requests package to run. I have searched around for answers, but it seems that the question is most often answered with instructions on installing the requests package, which you will see I have already done.
Before I start, my specs are as follows:
OS: MacOSX Catalina 10.15.7
Shell: iTerm2 Build 3.3.12
Python Versions:
System: 2.7.16
pyenv: 3.9.0
Use Case: I am experimenting with web scraping and am attempting to write a simple program that will scrape some data from a local car dealership's website (e.g. scrape info on cars listed under $20k).
Before starting the project, I installed the requests package with the following command:
% pip3 install requests
I then opened up a vim file and wrote the following, just to ensure that the requests package was working properly:
import requests
URL = 'cardealerurl.com/query'
page = requests.get(URL)
I saved the file as carScrape.py.
Then I went beck to the shell and executed the following:
python carScrape.py
on execution, I receive the following error message:
Traceback (most recent call last):
File "carScrape.py", line 1, in <module>
import packages.requests
ImportError: No module named packages.requests
Any help with this would be greatly appreciated. If more information is needed, please just let me know and I will provide whatever I can. Thank you all so much.
I was able to solve this by running the following commands:
% sudo -H pip3 uninstall pip
% python3 -m ensurepip
% pip3 install requests
This was after a pretty long chunk of time trying other solutions that did not work. I also had to start a new shell to get rid of the error message.

Using GNURadio in Python

I am trying to use GNURadio on Python 3.7. I am running on Windows 10 and using Anaconda.
Every time I try to import GNURadio, I get the following
import gnuradio
Traceback (most recent call last):
File "<ipython-input-3-9dfe3f3e2626>", line 1, in <module>
import gnuradio
ModuleNotFoundError: No module named 'gnuradio'
How can I import GNURadio in Python?
GNU Radio 3.7 is not compatible with Python3; end of story. Also, it's GNU Radio's "legacy/maintenance support release series"; don't use if for anything new! (update:) As of spring 2022, GNU Radio 3.7 is end-of-life and should not be used in anything but legacy systems.
GNU Radio 3.8 is compatible with both Py3 and Py2, but the popular GCN development GNU Radio Windows installer is built against Py2, so no, won't work for you either. 3.10 is available, and will be around for quite a while to enjoy. It is easily installable using your current Linux distro's package manager, or anaconda (on Linux, Mac OS X and Windows).
Installation instructions can be found in the GNU Radio wiki.

Import Error when running paho mqtt as root (sudo python3 filename.py)

I'm about to set up a Raspberry Pi 3+ Model A as a client for openHAB2. Here I have a sensor on the Pi and WS2812b (alias Neopixel) connected.
Now I wrote (my first script) in Python to publish the sensor data (Bosch BMP280) via paho mqtt to the broker. This works as desired when I start the Python file with "python3 filename.py" ...
If I want to start the same file with "sudo python3 filename.py" the following error message appears:
Traceback (most recent call last):
File "mqtt_client.py", line 15, in <module>
import paho.mqtt.client as mqtt
ModuleNotFoundError: No module named 'paho'
However, I would like to extend the script to use the WS2812b. To control the WS2812b, the script must be started with root privileges. So I do not understand why paho-mqtt is no longer known when running "sudo" ...
Does anyone have a solution for this? Many thanks!
When you are running your script using sudo python3 filename.py it is using a python environment different from the one you used to install Paho.
In order to fix this you can either:
Add Defaults env_keep += "PYTHONPATH" to your /etc/sudoers
Run your script using sudo /path/to/python pythonscript.py

Speech Recognition. Module installed but won't work

Trying to get the speech recognition module to work. I have it working on my Windows 10 laptop, my Raspberry pi3 but I can't seem to get it to work on Ubuntu! The module has been install but neither PyCharm or Thonny can find it.
I've searched through stack overflow for a solution and haven't been successful. I've tried uninstalling and reinstalling the module. I've tried various versions of installing the module such as using the setuup.py file or pip, tried moving the folders/files where they should be etc..
(Also tried telling the Computer he's a good boy but that didn't work either...)
I can use this line of code
python3 -m speech_recognition
With my mic plugged into the tower and it works, but when using a line in PyCharm or Thonny such as
import speech_recognition
print(speech_recognition.__version__)
I end up with
Traceback (most recent call last):
File "/home/elitree/SPRECOG/FU.py", line 1, in <module>
import speech_recognition
ModuleNotFoundError: No module named 'speech_recognition'
python version is 3.6
pip3 version is 19.1.1
Ubuntu version is Ubuntu 18.04.2 LTS
I can provide other system details if needed
I'm just not sure what to do now
As ForceBru explained to me - My IDE was using the wrong python interpreter.
Which is first identified by running the Python script in the terminal with python3 this_file.py
After this the problem could be fixed by identifying the correct interpreter using the command which python3 in the terminal and then configuring the IDE to use that interpreter.

string: syntax error on raspbian jelly, flawless on IDLE windows 8

My script (python 3.6) raises an error when executed on my RPI (Linux raspberrypi 4.9.59-v7+ #1047), but runs flawlessly when executed with IDLE, on a windows 8.1 PC.
Basically, I want to create a gzip archive from "data.json" file.
Here is a light version:
import time
import gzip
inputFile = "/path/to/data.json"
dataArchiveName = f"{inputFile}-{time.localtime().tm_mday}_{time.localtime().tm_mon}_{time.localtime().tm_year}.gz"
...
When I run the script, the following exception is raised:
File "dataManager2.py", line 96
dataArchiveName = f"{inputFile}-{time.localtime().tm_mday}_{time.localtime().tm_mon}_{time.localtime().tm_year}.gz"
^
SyntaxError: invalid syntax
I do not understand why... Any idea ?
Thank you in advance
f-strings were introduced in Python 3.6, since you are using Python 3.5 on you Raspberry Pi, you will have to either install Python 3.6 on it or use older way of formatting strings, this should work:
dataArchiveName = "%s-%s_%s_%s.gz" % (inputFile, time.localtime().tm_mday, time.localtime().tm_mon, time.localtime().tm_year)

Resources