I am developing web application through Django/Python framework on Raspberry Pi platform running Debian Linux and Python 2.7.9 .
I have to use multiple threads in Python script to handle multiple peripherals such as camera and microphone. These peripherals must be handled in real time.
I wonder why I can issue "import thread" but not "import threading" in my Python script (does not matter whether it is Python version 2.x or 3.x) ?
pi#raspberrypi:~ $ python --version
Python 2.7.9
pi#raspberrypi:~ $ python ./mythread.py
Traceback (most recent call last):
File "./mythread.py", line 4, in <module>
from threading import Thread
File "/home/pi/threading.py", line 8, in <module>
del _sys.modules[__name__]
AttributeError: 'module' object has no attribute 'Thread'
pi#raspberrypi:~ $ python3 ./mythread.py
Traceback (most recent call last):
File "./mythread.py", line 4, in <module>
from threading import Thread
ImportError: bad magic number in 'threading': b'\x03\xf3\r\n'
A couple of issues could exist, one of which, as #Alberto mentioned, is the fact that Python may be trying to used a pre-compiled, byte-code .pyc file. To avoid this, you can remove any pycache files from the related directory, which will then be recompiled when the interpreter tries to run them. The Bash code below will recursively remove .pyc file recursively from the current directory.
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
Related
I recognize that this is an installation failure on my part, and I'm sorry to lay this uninteresting and inconsequential question at your feet, but for the life of me I can't manage to figure out what is going wrong and I've run out of ideas. I'm hoping someone will be able to quickly point out the obvious.
I am trying to profile a python script (using Kern's line_profiler), and the script needs to load the netCDF4 module. I have installed both line_profiler and netCDF4 with pip. Both are reported as present and updated when I queue pip for the list of installed packages.
Without using the profiler, my script runs without problems, meaning that the netCDF4 module is loaded properly. However, if I run 'kernprof -l -v myscript.py' from the "myscript" directory, I get the following error:
Traceback (most recent call last):
File "/usr/local/bin/kernprof", line 9, in <module>
load_entry_point('line-profiler==1.0', 'console_scripts', 'kernprof')()
File "Library/Python/2.7/site-packages/kernprof.py", line 221, in main
execfile(script_file, ns, ns)
File "myscript.py", line 5, in <module>
from netCDF4 import Dataset
ImportError: No module named netCDF4
I am running Python from an installation at /opt/local/bin/python, which is listed first in my PATH.
So, in any case, if the default Python version that I have set is the same as that which appears first in my PATH, and that default version is able to access the netCDF4 module, why isn't line_profiler?
kernprof has a shebang that redirects to the default python install which doesn't have all the required modules.
You can force the use of your "complete" python install by doing:
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python /usr/local/bin/kernprof -l -v myscript.py
So shebang is ignored, and you run the profiler with the version of python containing all the required packages.
I am having an issue which has been driving me crazy. I am trying to run a python script as a non root user but when I try to execute the script I get the following error.
Traceback (most recent call last):
File "/usr/local/lib/EdgarRenderer/src/EdgarRenderer.py", line 13, in <module>
from arelle import PythonUtil # define 2.x or 3.x string types
ImportError: cannot import name PythonUtil
Now if I execute it as the root user it runs with out a hitch. I have triple checked all permissions and all the scripts and folders access by the desired user are in fact owned by that user (with the exception of the /usr/bin/python3.3 file). This is the command I am trying to execute
su - tomcat -c '/usr/bin/python3.3 /usr/local/lib/EdgarRenderer/src/EdgarRenderer.py -c /usr/local/lib/EdgarRenderer/conf/RunEdgar.xml --xdgConfigHome=/usr/local/lib/re3/arelle'
I run the exact same script the exact same way on another server with out any errors. I am using CentOS 6.5
Thanks!
The problem:
I have a Jenkins build step that executes a shell script. This script in turn calls a python script which is performing some cryptographic functions. However when the build executes, I am getting the following error.
Traceback (most recent call last):
File "./xyz.py", line 4, in <module>
import rsa
ImportError: No module named 'rsa'
The Jenkins' node has two versions of python - 2.7 (default) and 3.4 and rsa is installed for both of them. I even ran the script (using version 3.4) on the slave itself and it worked fine.
What I have done so far:
I am using EnvInject plugin to point PYTHONPATH to correct location. Without it, I found that PYTHONPATH was undefined.
With Python 2.7
Using default version, my script starts with: #!/usr/bin/env python
Jenkins output:
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content
PYTHONPATH=/usr/local/lib/python2.7
[EnvInject] - Variables injected successfully.
[demo] $ /bin/sh -xe /tmp/hudson9217742060700174209.sh
+ export PYTHONPATH=/jenkins/workspace/demo:/usr/local/lib/python2.7
+ echo /jenkins/workspace/demo:/usr/local/lib/python2.7
/jenkins/workspace/demo:/usr/local/lib/python2.7
+ ./abc.sh
/usr/bin/env: python: No such file or directory
With Python 3.4
The Shebang in this case is #!/usr/bin/env python3
Jenkins output:
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content
PYTHONPATH=/usr/local/lib/python3.4/
[EnvInject] - Variables injected successfully.
[demo] $ /bin/sh -xe /tmp/hudson4592372533933414288.sh
+ export PYTHONPATH=/jenkins/workspace/demo:/usr/local/lib/python3.4/
+ echo /jenkins/workspace/demo:/usr/local/lib/python3.4/
/jenkins/workspace/demo:/usr/local/lib/python3.4/
+ ./abc.sh
Traceback (most recent call last):
File "./xyz.py", line 4, in <module>
import rsa
ImportError: No module named 'rsa'
I even tried doing sys.path.append(os.environ['/usr/local/lib/python3.4/dist-packages/rsa']) in the script itself, but the problem persists.
Can anyone help me with this issue? Thanks.
P.S.- My knowledge of Python is very limited.
As far as I can remember it, I had similar problems with my Python-script and I finally solved it by using
python xyz.py
instead of
./xyz.py
Unfortunately I can not explain why it works in this way, but not the other, but in my case it solved the problem.
I'm running Debian 6.0.6
I've downloaded latest version of libtorrent-rasterbar from here: http://code.google.com/p/libtorrent/downloads/detail?name=libtorrent-rasterbar-0.16.6.tar.gz&can=2 and installed it:
./configure --enable-python-binding
make
make install
cd bindings/python
python setup.py build
python setup.py install
now I want to test whether the library works:
>>> import libtorrent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libtorrent-rasterbar.so.7: cannot open shared object file: No such file or directory
The packaged version of libtorrent from the distributive's repository had some funny behaviour (complaining on boost dependences), so I've decided to upgrade. The same situation is on both of my debian boxes, yet ubuntu box is fine.
old error message from my debian box:
File "ar.py", line 15, in create
s.start_dht()
Boost.Python.ArgumentError: Python argument types in
session.start_dht(session)
did not match C++ signature:
start_dht(libtorrent::session {lvalue}, libtorrent::entry)
Search for the shared object file and add it to your path.
sudo updatedb
locate libtorrent-rasterbar.so.7
This should output /<path_to_directory>/libtorrent-rasterbar.so.7
Add this to your ~/.profile or temporarily tell Python where to look:
export LD_LIBRARY_PATH=/<path_to_directory>/
See more detailed installation instructions here.
I am using Python 2.7 and Python 3.1.3. But in my Python I am unable to "import gdb".
It is giving me an error as:
>>> import gdb
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ImportError: No module named gdb
>>>
What's a reason for this? How should I solve this problem?
import gdb only works when your Python code is running within the GDB process. It's not supposed to work from the regular system Python interpreter.
Explanation
GDB embeds the Python interpreter so it can use Python as an extension language.
You can't just import gdb from /usr/bin/python like it's an ordinary Python library because GDB isn't structured as a library.
What you can do is source MY-SCRIPT.py from within gdb (equivalent to running gdb -x MY-SCRIPT.py).
Example Program
Here's a self contained example. Save the file below to t.py:
import gdb
gdb.execute('file /bin/cat')
o = gdb.execute('disassemble exit', to_string=True)
print(o)
gdb.execute('quit')
run:
$ gdb -q -x t.py
and you'll see the PLT stub for exit() disassembled. On x86-64 Linux:
Dump of assembler code for function exit#plt:
0x0000000000401ae0 <+0>: jmpq *0x20971a(%rip) # 0x60b200 <exit#got.plt>
0x0000000000401ae6 <+6>: pushq $0x3d
0x0000000000401aeb <+11>: jmpq 0x401700
End of assembler dump.
I've collected some resources on learning the GDB Python API here.
You can follow this tutorial to install PythonGDB. The Python code depends on a C extension.
For Windows, there is a recent enough gdb build in MinGW, but it doesn't seem to include the Python module you can import (still supports Python scripting in gdb). You have to install MinGW and then install the gbd package using mingw-get install gdb.
If you use Cygwin, there's again a recent enough gdb in Cygwin Ports, without a Python module but with Python scripting support.
I suppose it'd be possible to build gdb from source in either platform and get the Python module.
I just ran into the similar situation when trying to debug Webkit:
$ python Tools/gdb/webkit.py
Traceback (most recent call last):
File "Tools/gdb/webkit.py", line 38, in <module>
import gdb
ImportError: No module named gdb
I then realized that this script should be invoked in gdb to make it working:
(gdb) source Tools/gdb/webkit.py
(gdb) p run
$1 = (const WebCore::TextRun &) #0x7fffffffa450: {m_characters = "Plugin Testsa", m_len = 12, m_xpos = 0,
m_padding = 0, m_allowTabs = false, m_rtl = false, m_directionalOverride = false,
m_applyRunRounding = true, m_applyWordRounding = true, m_disableSpacing = false}
Hope this helps.
I can't test now, but I think you need to configure and build a Python-enabled GDB. Take a look at this guide.
I hope that helps.
This is outdated, I think. Anyway, you always need to build and configure a Python enabled GDB.
You can script GDB using the Python programming language. This feature is available only if GDB was configured using --with-python.
You have to configure GDB using that option:
--with-python=location
Where location is the location of python you would like to use GDB with.