I was using ast module of python3.4 to get the imports and function calls within a file.
It works correctly if I run the code on a file which has python3.4 syntax but throws an exception if I try to parse a file of older python2.7 version (for print statements, except statements which have a "," etc).
Is there a way to force ast to use python2.7 compiler while dealing with old files and use python3.4 compiler when dealing with python3.4 file?
Is there any other way to resolve this issue??
Turns out it is not possible to use different versions of AST parsers in python to the best of my knowledge. (It is still possible to parse them separately by carrying out multiple iterations each time using a different version AST)
Related
import MySQLdb
try:
dbcon = MySQLdb.connect(host=host_name, user=user_name,
passwd=password, db=db_name)
except MySQLdb.Error:
pass
getting this pylint warning
Module 'MySQLdb' has no 'Error' member (no-member)
The Best:
Using extension-pkg-whitelist option:
A comma-separated list of package or module names from where C extensions may be loaded. Extensions are loading into the active Python interpreter and may run arbitrary code
--extension-pkg-whitelist=_mysql
PyLint parses (by default) the source files, but in Python the shape of a module can change at runtime from the shape defined in the source file. This option tells PyLint to actually import the specified module, and use the runtime definition.
Note that since the MySQLdb package wraps a C extension, you have to pass the name of the C extension (_mysql) instead of the package name (MySQLdb). (source)
Not Bad:
Using unsafe-load-any-extension option
Allow loading of arbitrary C extensions. Extensions are imported into the active Python interpreter and may run arbitrary code.
--unsafe-load-any-extension=yes
You could use the unsafe-load-any-extension option, but that would load every available extension, with its' (potentially dangerous) initialization code. extension-pkg-whitelist is safer, because it only loads the specified modules.
The Worst:
Using disable option
# pylint: disable=no-member
It doesn't really solve the issue, but only makes PyLint silent.
Thanks to #PCManticore, the maintainer of PyLint. Here's the comment of the maintainer.
Thanks to #ZevSpitz, the contributor of the best answer and this not bad answer.
It may help to use the --extension-pkg-whitelist option:
--extension-pkg-whitelist=_mysql
pylint parses (by default) the source files, but in Python the shape of a module can change at runtime from the shape defined in the source file. This option tells pylint to actually import the specified module, and use the runtime definition.
Note that since the MySQLdb package wraps a C extension, you have to pass the name of the C extension (_mysql) instead of the package name (MySQLdb). (source)
You could use the unsafe-load-any-extension option, but that would load every available extension, with its' (potentially dangerous) initialization code. extension-pkg-whitelist is safer, because it only loads the specified modules.
I am in Python3. I have a bunch of Python2 .py files. For each of these files, I need to check whether the contents are valid Python2 code and return it as a boolean. The files are small scripts, sometimes they may import things or have a function or class, but mostly they are pretty simple.
How can I do this?
Do you do it at build time? If so, you can try run 2to3 and parse its output to determine if the file is valid Python 2 code.
I've already looked through the forums, but I haven't found an answer that is mavlink specific.
I am currently writing script in python, and I want to use a python module of mavlink.
The documentation for mavlink tells me to run mavgenerate.py from my mavlink folder. When I run this script, a gui appears asking for my xml files, a specified output directory, and what language I want my headers in. mavgenerate.py works when I choose to make headers for C, but it gives me the error: "attempted relative import in non-package" when I try to choose python.
my xml files is located at:
C:\Python27\mavlink\message_definitions\v1.0
and I have my python module output directory as:
C:\Python27\mavlink\pymavlink\include
below is a screenshot of my error.
can anyone tell me what I'm doing wrong?
This is quite an old question but i have just encountered with this problem. Solution that i have applied is: in mavgenerate.py replace following lines
sys.path.append(os.path.join('pymavlink','generator'))
from mavgen import *
with
from pymavlink.generator.mavgen import *
I dont know the exact reason of the problem but from
this thread i assume problem lies with python's interpretation of directories as packages.
I have used python 2.7. Tried though to do with python 3.4 but didn't had chance there.
I have a decently long program that I have been trying to compile. I have tried py2exe and cx_Freeze, both seem to come up with this problem.
I used the following setup.py file to compile my program:
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [
Executable('version_3_2.py', base=base)
]
setup(name='version_3_2',
version='0.32',
description='desc',
executables=executables
)
From running this using
python setup.py build
The executable is created.
From running the executable, i was given a traceback stating that
TclError: Can't find a usable init.tcl in the following directories:
C:/Python27/build/lib/tcl8.5
and a bunch of other directories
From adding all of the tkinter and tcl files and folers into a couple of those directories i get the next traceback from executing:
C:/Python27/build/lib/tcl8.5/init.tcl: version conflict for package "Tcl":
have 8.5.15, need exactly 8.5.2
version conflict for package "Tcl": have 8.5.15, need exactly 8.5.2
while executing
"package require -exact Tcl 8.5.2"
(file "C:/Python27/build/lib/tcl8.5/init.tcl" line 20)
invoked from within
"source C:/Python27/build/lib/tcl8.5/init.tcl"
("uplevel" body line 1)
invoked from within
"uplevel #0 [list source $tclfile]"
I'm not entirely sure what to do. Several solutions like How to correct TCL_LIBRARY and TK_LIBRARY with py2exe and Py2exe with Tkinter have not worked.
Any Ideas?
You need to match exactly the libtcl8.5.dll (or whatever the name is on your system) and the init.tcl (and related files) because they are both part of the same Tcl installation. If you change one out, you must change the other as well. How exactly you've mangled your installation I'm not quite sure, but mangled it surely is. Also be aware that in Tcl 8.5 (and before), it's strongly advised to match the Tk version precisely to it as well; we relax this requirement in 8.6 but you're not using that…
(Note that this is different from the way that normal dependencies work in Tcl; typically a program will depend on a minimum API version, not an exact one.)
Since you've got a binary build of 8.5.15 in use, you might as well use the script files for that version too. I suggest getting the Tcl source distribution for 8.5.15 from SourceForge and grabbing the files out of the library directory in there. You probably only need the .tcl files directly in there (especially init.tcl of course!) and not in any of the subdirectories, as the subdirectories are for official add on packages and those aren't so tightly bound to Tcl versions.
VTK offers the possibility to extend the library with C++ classes so that they can be used in Python (or in any language supported). As far as I know, there are two way to do that, using the examples folders vtkLocal or vtkMy provided with the library (Examples/Build/).
I would like to use this functionnality, but I don't understand the installation process (vtk 5.8 on Debian). I follow the README instruction on vtkMy, it compiles and generetate 3 files : vtk*.so vtk*Python.so and vtk*PythonD.so.
What I am suppose to do next ? I've tried appending those file's path to $PATH, $PYTHONPATH or $LD_LIBRARY_PATH as suggested, but I've never been able to import anything into Python.
Any insight or even some instructions on how to compile/import/use the dummy classes provided in vtkMy would be a tremendous help.