I have a python module (getActiveLocation.py). It basically makes a http POST call, processes its response and returns it.
It makes use of json module. I am running python 2.6 on RedHat Linux. This module works fine when it is run standalone.
When the module getActiveLocation.py is run standalone, sys.path is
['/current_directory/','/usr/lib64/python26.zip','/usr/lib64/python2.6','/usr/lib64/python2.6/lib-tk','/usr/lib64/python2.6/site-packages','/usr/lib/python2.6/site-packages']
json module is existing within /usr/lib64/python2.6/ directory.
This python module (getActiveLocation.py) is imported inside a jython script (schedule_location.py) and when jython script is run, it is giving an import error for json module.
ImportError: no module named json
It looks like a sys.path issue.
I tried manually adding /usr/lib64/python2.6 to sys.path inside schedule_location.py via:
sys.path.append('/usr/lib64/python2.6')
Then the error changed to:
File "/usr/lib64/python2.6/json/__init__.py", line 108
from .decoder import JSONDecoder
^
SyntaxError: invalid syntax
Any idea why this error is happening? Your help is appreciated.
Vasily,
I am on a production system where I do not have any control! So upgrading jython is not an immediate solution for me :(.
I used simplejson as mentioned in the comment in Portable json module in jython
But here again, it is working when I run it in python. But when run from jython, it gives:
File "schedule_location.py", line 21, in ?
File "getActiveLocation.py", line 4, in ?
File "simplejson/__init__.py", line 113, in ?
File "simplejson/decoder.py", line 7
from .compat import fromhex, b, u, text_type, binary_type, PY3, unichr
^
SyntaxError: invalid syntax
To overcome this, I used:
from __future__ import absolute_import
But it gave:
File "schedule_location.py", line 21, in ?
File "getActiveLocation.py", line 1
SyntaxError: future feature absolute_import is not defined
I understand that this requires python 2.5 or more. But I am on Python 2.6
Probably you need
from __future__ import absolute_import
to make your code compatible with Python 3.x style imports.
Or try to remove "dot": from decoder import JSONDecoder. It should work in Python 2.6. Not sure about Jython.
EDIT: Found answer in the comment here: Portable json module in jython
I had the same SyntaxError when running a python script from a bash script. The solution was to specify the PYTHONPATH, e.g. PYTHONPATH = /usr/local/lib/python2.7. I also specified which python to use when running the python script, e.g. /usr/local/bin/python my_python_script.py.
Related
There is a custom module "ETPython" generated by SWIG (consists of ETPython.py and binary _ETPython.so) and a simple script that invokes this module
sample.py
import ETPython
...
There aren't any problems if the script is run in an IDE (pycharm, internal python's IDLE, squish by froglogic so on). But if I'm trying to launch it in python shell in interactive mode or via terminal using
python3 sample.py
there is an error message like:
File "<path_to_file>/example.py", line 12, in <module>
import ETPython
File "<path_to_file>/ETPython.py", line 15, in <module>
import _ETPython
ImportError: dlopen(<path_to_file>/_ETPython.so, 2): Symbol not found: _NSLog
Referenced from: <path_to_file>/_ETPython.so
Expected in: flat namespace
Searching topics, I found that problem is related to wrong paths. So I added some code:
import os, sys
os.chdir("<path_to_dir>")
sys.path.append('<path_to_dir>')
os.system('export PYTHONPATH=<path_to_dir>:$PYTHONPATH')
This code helped to import the module in python shell in interactive mode but launching in terminal is still failing.
So the question is how to make it to work?
I found solution. The SWIG module was compiled incorrectly.
There was option for CMake that suppressed errors for undefined symbols
set(PLATFORM_LIBS "-undefined dynamic_lookup" )
That why the module doesn't contain NSLOG symbol.
The module was recompiled with additional
"-framework Foundation"
in swig_link_libraries statement. And now the module is imported correctly
I am trying to run this python rewrite of Vlfeat library.
https://github.com/shackenberg/phow_caltech101.py. I am trying to run the application phow_caltech101.
This is throwing
File "/A/B/C/pyvlfeat-0.1.1a3/vlfeat/__init__.py", line 1, in <module>
import _vlfeat
ImportError: No module named _vlfeat
In the corresponding "init.py" file, I can see it is mentioned as "import _vlfeat". I am new to python, please let me know what is causing this error?
You need to download and install PyVlfeat module.
https://pypi.python.org/pypi/pyvlfeat/
As I see, pyvlfeat has some dependencies, so be sure to download these too:
Boost.Python (tested against version 1.35.0-5)
NumPy (tested against version 1.5.1)
Matplotlib (tested against version 0.99.3)
I'm getting the following error while running the python program. I'm using python 3.4 and I have installed Ferenda-0.1.7-py3.4 And the error is:
File "C:\Python34\lib\site-packages\ferenda-0.1.7-py3.4.egg\ferenda\util.py", line 20, in module
from ast import literal_eval
ImportError: cannot import name 'literal_eval'
Appreciate you help on this. Thank you.
When I tried running in the command prompt. I can see no errors. Attached is the picture
You have named your script ast.py (or you have another script in the same directory named ast.py) and this script does not contain a literal_eval.
Im trying to compile Godot engine following the instructions here
When I run scons bin/godot as the tutorial says, I get the following error:
scons: Reading SConscript files ...
ImportError: cannot import name _args_from_interpreter_flags:
File "/home/grayfox/github/godot2/godot/SConstruct", line 9:
import multiprocessing
File "/usr/lib64/python2.7/multiprocessing/__init__.py", line 65:
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/usr/lib64/python2.7/multiprocessing/util.py", line 40:
from subprocess import _args_from_interpreter_flags
The SConstruct file starts this way:
EnsureSConsVersion(0,14);
import string
import os
import os.path
import glob
import sys
import methods
import multiprocessing
...
If I try to run python SConstruct I get an error complaining about missing functions defined by scons (i.e. the script fails after doing all the imports).
Commenting import multiprocessing fixes the issue but I don't want to modify that file, as I would have to revert the change if I ever make a pull request. The project is quite active so I believe this has something to do with my local configuration.
Any ideas why the script is failing to import _args_from_interpreter_flags only if I execute it via scons?
[UPDATE]
I did a fresh Gentoo install and the problem persists. I did some tests and I found this:
In a python terminal>
>>> import SCons.Script
>>> from subprocess import _args_from_interpreter_flags
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name _args_from_interpreter_flags
>>> import subprocess
>>> subprocess.__file__
'/usr/lib64/python2.7/site-packages/SCons/compat/_scons_subprocess.pyc'
But the output is different if I do this:
>>> import subprocess
>>> subprocess.__file__
'/usr/lib64/python2.7/subprocess.pyc'
So I update my question: Is this a bug? Can anybody reproduce it in other distros? If it's a bug, should I report it to Gentoo or to SCons?
[ANOTHER UPDATE]
Adding temp.extend([os.path.join(x, 'lib64') for x in prefs]) did't work, same error.
Adding print sys.path at the beginning of the compact module gives:
['/usr/lib64/python-exec/python2.7/scons-local-2.3.0',
'/usr/lib64/python-exec/python2.7/scons-local',
'/usr/lib64/python2.7/site-packages/lib32/scons-2.3.0',
'/usr/lib32/scons-2.3.0',
'/usr/local/lib32/scons-2.3.0',
'/usr/lib64/python2.7/site-packages/lib/python2.7/site-packages/scons-2.3.0',
'/usr/lib/python2.7/site-packages/scons-2.3.0',
'/usr/local/lib/python2.7/site-packages/scons-2.3.0',
'/usr/lib64/scons-2.3.0',
'/usr/lib64/python2.7/site-packages/lib32/scons',
'/usr/lib32/scons',
'/usr/local/lib32/scons',
'/usr/lib64/python2.7/site-packages/lib/python2.7/site-packages/scons',
'/usr/lib/python2.7/site-packages/scons',
'/usr/local/lib/python2.7/site-packages/scons',
'/usr/lib64/scons',
'/usr/lib64/python2.7/site-packages/RBTools-0.6-py2.7.egg',
'/usr/lib64/python27.zip',
'/usr/lib64/python2.7', #It's here, so what's the problem?
'/usr/lib64/python2.7/plat-linux2',
'/usr/lib64/python2.7/lib-tk',
'/usr/lib64/python2.7/lib-old',
'/usr/lib64/python2.7/lib-dynload',
'/usr/lib64/python2.7/site-packages',
'/usr/lib64/python2.7/site-packages/gtk-2.0',
'/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode']
It looks as if this isn't really a problem connected to SCons directly. You might have an alien "subprocess" module/package installed in your system. Also check out Cannot import name _args_from_interpreter_flags which seems to be related.
Based on your updated question: I tried to compile Godot on my machine (Python 2.7.3, SCons 2.3.1, Ubuntu 12.04 LTS) and it's running fine, so the problem is not related to the provided SConstruct (and its supporting build description files in subfolders). The "_scons_subprocess" module gets used only when the import of the original "subprocess.py" fails. So I suspect that the SCons start script sets up a wrong sys.path, which may happen under 64bit (see issue http://scons.tigris.org/issues/show_bug.cgi?id=2657 ).
After you added "temp.extend([os.path.join(x, 'lib64') for x in prefs])", your "print sys.path" statement shows paths like "/usr/lib64/python-exec" in its output. A Google search turned up the page http://forums.gentoo.org/viewtopic-t-985402-start-0.html for me. It describes an issue with Gentoo, where programs are installed as links to pip. Please follow the given advice and see if this fixes your problem.
It's a bug in Gentoo's scons-2.3.0 and scons-2.3.1 ebuilds (see bug report). It has been fixed in versions 2.3.1-r1 and higher.
I have a software that has python 2.5.5. I want to send a command that would start a script in python 2.7.5 and then proceed with the script.
I tried using
#!python2.7.5
and http://redsymbol.net/articles/env-and-python-scripts-version/
But I cant get it to work...
In my python 2.5.5 I can execute script as
execfile("c:/script/test.py")
The problem is that the 2.7.5 has a module comtypes + few other. I dont know how to install it for my 2.5.5 so I'm trying to start a separate script and run it under python27. Now another reason why I want it its because I want to take the load off program. I have 2 heavy tasks to perform. The second task is the one that need comptypes so sending it to external shell/app would do perfect trick. Is there a way to do it ?
I wish I could just type run("C:/Python27/python.exe % C:/script/test,py")
Thanks, bye.
Little update. I try to run
import os
os.system("\"C:\Python27\python.exe\" D:\test\runTest.py")
But I'm getting a quick pop up and close window saying that
Import Error : no module named site...
This works if I run from external shell but not from here :(
So I've tried another approach this time to add modules to python... in any case I run this :
import os
import sys
sys.path.append("C:/python27")
sys.path.append("C:/Python27/libs")
sys.path.append("C:/Python27/Lib")
sys.path.append("C:/Python27/Lib/logging")
sys.path.append("C:/Python27/Lib/site-packages")
sys.path.append("C:/Python27/Lib/ctypes")
sys.path.append("C:/Python27/DLLs")
import PyQt4
print PyQt4
import comtypes
import logging
but it crashes with C error...
Runtime Error :
Program: c:\Pr...
R6034
An application has made attempt to load the C runtime library incorectly.
blablabla....
How can I import it ? Maybe if I can import it I can run it directly from my app rather than starting separate python...
Traceback (most recent call last):
File "<string>", line 18, in <module>
File "C:\Python27\Lib\site-packages\comtypes\__init__.py", line 22, in <module>
from ctypes import *
File "C:\Python27\Lib\ctypes\__init__.py", line 10, in <module>
from _ctypes import Union, Structure, Array
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
Another update to isseu
so I run now
import os
os.system("start cmd {D:\test\runTest.py}")
now this works and he open CMD with c:\Python27 as directory but he dont run the file... any hitns how to fix it?
Use "raw" strings so that you don't need to escape as much; I think the backslashes are what was breaking your code since backslash is considered an escape character except in raw strings.
Also, use the subprocess module. It makes it easy to avoid manually making a safe command string (the module takes care of that for you). All you need to do is pass it a list of arguments.
Your code would then look something like this:
import subprocess
proc = subprocess.Popen([r"C:\Python27\python.exe", r"D:\test\runTest.py"])
# then either do this
proc.wait() # wait until the process finishes
# or this
while True:
# NOTE: do something else here
# poll the process until it is done
if proc.poll() is not None:
break # break out of loop
See subprocess docs for Python 2 here. Be sure to check if a feature was added after Python 2.5 (the 2.5 docs aren't available online anymore AFAIK).
UPDATE:
I just noticed that you tried to use the Python 2.7 libraries and modules in your 2.5 code. This probably won't work due to new features added after 2.5. But it got me thinking how you might be able to make 2.7 work.
It may be that your Python2.7 install can't find its libraries; this is probably why you get the error Import Error : no module named site. You can do something like the above and modify the PYTHONPATH environment variable before starting the subprocess, like this:
import os
import subprocess
paths = [r"C:\python27", r"C:\python27\libs", r"C:\python27\Lib\site-packages", r"C:\python27\DLLs"]
paths += os.environ.get('PYTHONPATH', '').split(os.pathsep)
env27 = dict(os.environ)
env27['PYTHONPATH'] = os.pathsep.join(paths)
proc = subprocess.Popen([r"C:\Python27\python.exe", r"D:\test\runTest.py"], env=env27)