Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import ?
#1
My first python application
I have some understanding of "import" - as similar to C "include".
I can see how it works in one project / application, however , I cannot figure out what needs to be done to "import" module from another project.

My real application / example

My main application just opens a simple window.
That window will setup bluetooth communication.

I have cloned PyBluez and now want to

import bluetooth

into my main application


Actually a tutorial how to "sort of link python applications" would be nice.

I have not found a single doc on how to add "import" functionality to python application.
Reply
#2
https://docs.python.org/3/tutorial/modules.html
https://python-forum.io/Thread-Basic-Modules-part-1
Reply
#3
Thanks, however, I was not looking for definitions.

Here is a sample code I pickled from pybluez

import bluetooth

print("Performing inquiry...")

nearby_devices = bluetooth.discover_devices(duration=8, lookup_names=True,
flush_cache=True, lookup_class=False)

print("Found {} devices".format(len(nearby_devices)))

for addr, name in nearby_devices:
try:
print(" {} - {}".format(addr, name))
except UnicodeEncodeError:
print(" {} - {}".format(addr, name.encode("utf-8", "replace")))




And here is the error I am getting




traceback (most recent call last):
File "/home/z/git/pybluez/examples/simple/inquiry.py", line 11, in <module>
import bluetooth
ModuleNotFoundError: No module named 'bluetooth'


My real question is - how do I add / insert "bluetooth" module ?
I have no instruction on HOW to do that.

In other words - do I need to add "bluetooth library" or what to my IDE ?
From where?

I have another commercial python application using bluetooth and it works just fine.
That tells me Linux has required bluetooth packages.

What am I missing in my IDE to build my own code ?
Reply
#4
A module can be as simple as a single python file. By default, it looks for modules in your PYTHONPATH (to view it at runtime, do import sys and then print(sys.path).

If a <module>.py file is in there, or if you have a <module> directory with the right construction, it will be imported.

The "6.1.2 module search path" and "6.4 Packages" (for understanding the PyBluez setup) are important bits from the links in Yoriz's post. They're not just definitions.

$ echo 'print("Hello, I am a module")' > foo.py
$ python3
Python 3.8.2 (default, Feb 27 2020, 15:22:23)
[Clang 11.0.0 (clang-1100.0.33.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
Hello, I am a module
Reply
#5
You need to install the bluez module first

https://pypi.org/project/PyBluez/

pip install bluez

Keep it simple at first..
Exit IDE.
Go to command prompt, and cd to your Python main dir.
Look in /Lib/Site-Packages for the PyBluez *and* bluetooth dirs.
If not there, look in your users dir, typically under AppData/Roaming/Python for Win, and I believe /Home/.local in Linux (not in Linux now..)

Then go to a Python prompt or idle and type:
import bluetooth
If you get an error it means that there was an install problem.

Modules are normally installed with 'pip install' these days, or if using Anaconda, 'Conda install'. Pypi.org is the best place to hunt for modules.

You can install modules downloaded from git with:
python setup.py install

Also.. I've noted that some IDEs need to be configured for Python.
Among the tutorials I've seen lately (still a n00b), Jupyter and PyCharm seem to be the most popular (though I use Notepad/Gedit).
Reply
#6
I have two python interpreters I can select.
I did check both for PyBlueZ BUT did not realize that the application I have cloned from github was build for pyhton2.7 !
After adding 2.7 the code partially works - imports the "bluetooth", but now I am getting another error down the line.
I am planning to copy the "inquiry.py" code to my application running with 3.7.

Traceback (most recent call last):
  File "/home/z/git/pybluez/examples/simple/Traceback (most recent call last):
  File "/home/z/git/pybluez/examples/simple/inquiry.py", line 11, in <module>
    import bluetooth 
  File "/home/z/.local/lib/python2.7/site-packages/bluetooth/__init__.py", line 282, in <module>
    """
AttributeError: attribute '__doc__' of 'instancemethod' objects is not writable
", line 11, in <module>
    import bluetooth 
  File "/home/z/.local/lib/python2.7/site-packages/bluetooth/__init__.py", line 282, in <module>
    """
AttributeError: attribute '__doc__' of 'instancemethod' objects is not writable
Reply
#7
Look at \docs\install.rst in the bluez source.

To compile you need VC 2014 *and* Python 3.5

Forget about the git file.

Go to Pypi.org and 'pip install bluez' from there, and make sure that Python3.7\Scripts is in the path *before* Python 2.7\Scripts.

OK. I see you are in Linux.
Did you try 'apt-get install bluez'? (Or the equivalent for the distro).
Check /usr/bin to see the paths for pip and python executables. You may need to call Python3.7 as pyrhon3 and/or pip (for python3.7) as pip3. Make symlinks as necessary.

In your python scripts you would set the environment at the hash-bang:
#!/usr/bin/python3 (or python2)

Python 2.7 is great for legacy code, but 3.x can handle most of that with the six and 2-3 modules.

And whenever running into permissions problems either run as root or sudo the command and see if the problem persists.
Reply
#8
@anne here im sharing the link that will helps you definitely.
Link : https://docs.python.org/3/tutorial/modules.html
Reply
#9
This is getting pretty convoluted.

Can I include file here instead of text ? ( see what follows )

What is the difference between using "pip " to install and plain "apt install"?
It looks as the original issue was and still is mixing interpreter versions.

BTW - I did borrow a Python book from my grand kid teenager and I can read. When I asked him about his opinion of the book - he replied - "It is OK, but it should not take five chapters to explain "print " .
I "learn" from others code , reinventing wheel is not my forte. So I will not read the book from cover to cover, just pick what I need as I go .

I have a working C code doing what I want to do in python - inquire about nearby bluetooth devices. I have downloaded / installed and implemented C version of BlueZ.

Not so sure why I am having issues doing same in pyhton.

Here is what I get "installing " bluez:
( Sorry - it is a messy read, please concentrate on "bluez" )

z@z-desktop:~$ sudo pip install bluez
[sudo] password for z:
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/developmen...-2-support
WARNING: The directory '/home/z/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
ERROR: Could not find a version that satisfies the requirement bluez (from versions: none)
ERROR: No matching distribution found for bluez

z@z-desktop:~$ sudo pip3 install bluez
The directory '/home/z/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/z/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting bluez
Could not find a version that satisfies the requirement bluez (from versions: )
No matching distribution found for bluez
You are using pip version 8.1.1, however version 20.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.



z@z-desktop:~$ sudo apt install bluez
Reading package lists... Done
Building dependency tree
Reading state information... Done
bluez is already the newest version (5.37-0ubuntu5.3).
0 upgraded, 0 newly installed, 0 to remove and 34 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] ^Y TYPO



z@z-desktop:~$ sudo apt install bluez
Reading package lists... Done
Building dependency tree
Reading state information... Done
bluez is already the newest version (5.37-0ubuntu5.3).
0 upgraded, 0 newly installed, 0 to remove and 34 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
dpkg: warning: parsing file '/var/lib/dpkg/status' near line 43815 package 'tomtomsportsconnect':
missing maintainer
Setting up runit (2.1.2-3ubuntu1) ...
start: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
dpkg: error processing package runit (--configure):
subprocess installed post-installation script returned error exit status 1
E: Sub-process /usr/bin/dpkg returned an error code (1)
z@z-desktop:~$


PS
I did "upgrade" pip several times and it still starts with older version and suggest to "upgrade"


PPS
I am not getting PM e-mails.
How do I change running thread to "just e-mails" ?
Reply
#10
Sorry, I marked the thread solved and prompt lost my message !

My "inquire.py" code found my bluetooth devices AFTER I selected CORRECT version of interpreter .
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020