Unable to import RRDtool in Python - python

I am trying to import RRDtool into Python as I want to access an RRD database using Python, but when I am trying to import rrdtool I am getting the following error.
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/opt/rrdtool-1.4.5/bin')
>>> import rrdtool
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named rrdtool
My RRDtool is located in /opt/rrdtool-1.4.5/bin.

Well, the problem is solved just by executing the following command.
sudo apt-get install python-rrd

You probably need py-rrdtool, which you could get directly from the site or your package manager.

It is unlikely that Python modules are located inside a 'bin' folder. And importing files from some configured path requires that the referred path is a proper Python package. It means it must contain an __init__.py file.

You could use the RRDtool documentation for inspiration (man rrdpyton). Something
along the lines of
import sys
sys.path.append('/path/to/rrdtool/lib/python2.6/site-packages/')
import rrdtool
should do the trick.

Related

Can't import torch in Python script

So I wanted to try out torch today, but I have the somewhat confusing problem that I can only use it in a Python console but not when I run a script that contains the same code.
In the command prompt it works just fine:
C:\Users\USER>python
Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
1.10.1+cu113
>>>
But when I run the exact same code from a .py file, it looks like this:
PS [USER]#D:\Benutzer\USER\Desktop\torch>python .\torch.py
Traceback (most recent call last):
File ".\torch.py", line 1, in <module>
import torch
File "D:\Benutzer\Julius\Desktop\torch\torch.py", line 5, in <module>
print(torch.__version__)
AttributeError: partially initialized module 'torch' has no attribute '__version__' (most likely due to a circular import)
I actually tried to install torch via Anaconda before, which produced the exact same error, which is why I now tried to use it with plain Python, where I installed torch via PIP. (I also had to downgrade Python from 3.10.1 to 3.8.10 because PIP would'nt find torch)
When I run it in the VSCode debugger, it also looks like the import doesn't work correctly, because it shows this:
I suspect there is some kind of circular import problem happening, but I don't understand why. The code is the exact same as in the console. There is only a single import statement in the file: import torch
So, while writing this question and sanity checking myself it suddenly hit me:
I named the file torch.py.
So it tried to import itself. (I'm not terribly familiar with Python imports, I didn't think it would look for local files)
TL;DR: DON'T call your script file name exactly as a lib import.

Difference between importing module in Python 3.4 and Python 2.7

I am trying to import a package present in another folder and it is working perfectly fine in python 3.4. For Example: the files are present in libraries folder
user> python
Python 3.4.1 (default, Nov 12 2014, 13:34:29)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from libraries.controller_utils import *
>>>
However when I open a new shell and use Python 2.7:
user> python
Python 2.7.4 (default, Jun 1 2015, 10:35:58)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from libraries.controller_utils import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named libraries.controller_utils
>>>
I tried adding the entry to sys.path but it is not helping. I read a similar question here but the solution is not helping me either as I tried both relative and absolute imports. Please advice.
EDIT: The directory structure being ~/tests/libraries/controller_utils.py. I am executing these commands inside the tests directory.
EDIT: I have added the sys.path entry as follows but it still does not recognize it. Please note that the error occurs on 2.7 but works absolutely fine on 3.4
user> cd ~/tests/
user> ls
__pycache__ backups inputs libraries openflow.py test_flow.py
user> ls libraries/
__pycache__ controller_utils.py general_utils.py general_utils.pyc tc_name_list.py test_case_utils.py
user> python
Python 2.7.4 (default, Jun 1 2015, 10:35:58)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from libraries.controller_utils import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named libraries.controller_utils
>>> import sys
>>> sys.path.append('libraries/')
>>> from libraries.controller_utils import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named libraries.controller_utils
Your libraries package is missing the __init__.py file. You can create an empty file with that name and then:
from libraries.controller_utils import *
should work.
Alternatively, if you don't want to turn libraries into a package you should add its path to sys.path and import controller_utils:
import sys
sys.path.append('libraries/')
from controller_utils import *
Note that the error is due to the fact that python2 requires the existence of __init__.py to import from packages while python3.3+ provides namespace packages (see PEP420). That's why the import doesn't fail in python3.4.
If you want your code to work in both python2 and python3 in the same way you should always add __init__.py file to packages and use from __future__ import absolute_import in your files.
See PEP 0404.
In Python 3, implicit relative imports within packages are no longer available - only absolute imports and explicit relative imports are supported. In addition, star imports (e.g. from x import *) are only permitted in module level code.
If libraries was in the same directory this would have happened to avoid clashes with packages installed at the system level. It would have been an implicit relative import.
You should be able to navigate to the correct location of the module using .., like:
from ..libraries.controller_utils import *
# or, depending of you directory structure
# put as many dots as directories you need to get out of
from ....common.libraries.controller_utils import *
But your case seems to be related to the star import. In Python 3 you can only use star imports (from x import *) at the top level of the file, i.e. not inside a function or class definition.

Problems in pexpect (python3.3)

Running 3.3 python on CentOS 7.
Tryin' to write simple script but can't get pexpect module to work as I want
if I use interpreter python 3.3, I can write this commands correctly
[root#localhost expect]# python3.3
Python 3.3.3 (default, Apr 7 2015, 02:31:24)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> child = pexpect.spawn('telnet 10.1.1.1')
but If I run file pexpect.py with exactly same commands, I get
[root#localhost expect]# python3.3 /usr/etc/pexpect.py
Traceback (most recent call last):
File "/usr/etc/pexpect.py", line 1, in <module>
import pexpect
File "/usr/etc/pexpect.py", line 3, in <module>
child = pexpect.spawn('telnet 10.1.1.1');
AttributeError: 'module' object has no attribute 'spawn'
I found some similar info in the google, advice was to move .py file to another folder.
It didn't work for me.
Another advice was to delete " pycache" folder (I've got same in my pexpect.py location), but it didn't work aswell. Errors are still the same, this folder are still created after running the script (trying, I mean).
Any ideas?
You have called your file pexpect.py. You need to rename it to something else as you are importing from your file not the pexpect module. You also need to delete any .pyc in the same folder. It does not matter where you move your script, the current folder is still going to be in the path before where the actual pexpect module is.

qrcode for python on linux

I have an error when i used pyqrcode.
[root#localhost python2.6]# python
Python 2.6.5 (r265:79063, Sep 7 2010, 07:31:57)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import qrcode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/site-packages/qrcode-0.2.1-py2.6-linux-x86_64.egg/qrcode/__init__.py", line 6, in <module>
from qrcode import _qrcode
ImportError: cannot import name _qrcode
How to resolve above error?
I am referring pyqrcode from http://pyqrcode.sourceforge.net/
Thanks,
Manu
After the installation of PIL-1.1.7 and JCC-2.14, I've tried to install pyqrcode-0.2.1 from sources as you did, and also ran into the same error :
ImportError: No module named _qrcode. But then I've noticed that _qrcode is actually a lib (_qrcode.so). So I've tried to add it on my library path :
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/python2.7/site-packages/qrcode-0.2.1-py2.7-linux-x86_64.egg/qrcode/
And it worked ! Well actually, not quite, I ran into another error :
AttributeError: 'module' object has no attribute '_setExceptionTypes'
So I've edited the __init__.py file
# probably located under a path like this for linux
/usr/local/lib/python2.7/site-packages/qrcode-0.2.1-py2.7-linux-x86_64.egg/qrcode/
# or under a path like this for a Mac
/Library/Python/2.7/site-packages/qrcode-0.2.1-py2.7-macosx-10.7-intel.egg/qrcode/
and commented out line 21 :
# _qrcode._setExceptionTypes(JavaError, InvalidArgsError)
Then I was able to run their simple example :
#!/usr/bin/env python
# coding: utf-8
#
# pyqrcode sample encoder
import sys, qrcode
e = qrcode.Encoder()
image = e.encode('woah!', version=15, mode=e.mode.BINARY, eclevel=e.eclevel.H)
image.save('out.png')
(source : http://pyqrcode.sourceforge.net/)
Hope it helps,

"from _json import..." - python

I am inspecting the JSON module of python 3.1, and am currently in /Lib/json/scanner.py. At the top of the file is the following line:
from _json import make_scanner as c_make_scanner
There are five .py files in the module's directory: __init__ (two leading and trailing underscores, it's formatting as bold), decoder, encoder, scanner and tool. There is no file called "json".
My question is: when doing the import, where exactly is "make_scanner" coming from?
Yes, I am very new to Python!
It's coming from a C-compiled _json.pyd (or _json.so, etc, etc, depending on the platform) that lives elsewhere on the sys.path. You can always find out where that is in your specific Python installation by importing the module yourself and looking at its __file__, e.g.:
>>> import _json
>>> _json.__file__
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/_json.so'
As you see, in my installation of Python 2.6, _json comes from the lib-dynload subdirectory of lib/python2.6, and the extension used on this platform is .so.
It may be coming from a file, or it may be built-in. On Windows, it appears to be built-in.
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import _json
>>> _json.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
and there is no _json.pyd or _json.dll in the offing.
If you want to see the source, having a binary file on your machine or not is irrelevant -- you'll need the SVN browser.

Categories