I'm writing a crossplatform python script on windows using Eclipse with the Pydev plugin. The script makes use of the os.symlink() and os.readlink() methods if the current platform isn't NT.
Since the os.symlink() and os.readlink() methods aren't available on the Windows platform Pydev flags them as undefined variables--like so:
Question:
Is there a way to ignore specific undefined variable name errors without modifying my source file?
edit: I found a way to ignore undefined variable errors from this answer on stackoverflow.
I'll leave the question open in case there is a way to solve this using project file or Pydev setting.
I use pydev + pylint.
With pylint you can add which messages to ignore in the Preferences>Pydev>Pylint>"Aggruments to pass to pylint" section.
--disable-msg=W0232,F0401
You can ignore messages in-line as well with comments:
os.symlink(target, symlink) # IGNORE:<MessageID>
Mouse-over the "x" where the line numbers are to see the message id.
I suspect pydev may have better, specific solutions, but what about just putting some code at the start of your program, such as:
if not hasattr(os, 'symlink'): os.symlink = None
Yeah, it's a hack, but, unless pydev does have specialized solutions (unfortunately I don't know of any, but then I'm no pydev expert;-), may be better than nothing...
I noticed PyDev doesn't recognize ZeroMQ constants so I struggled with the same problem.
I found PyDev has a settings option in Preferences > PyDev > Code Editor > Code Analysis : Undefined-tab. Just write symlink and readlink there (comma separated) to remove the errors.
Still not optimal, but good enough for now.
Related
I am trying to learn Enaml, which is an extension to the Python language that allows you to define hierarchical trees of objects used for graphical user interfaces.
Since enaml is a super-set of the Python language, its syntax can be different from Python's one and IDEs do not highlight it.
Is there any way to get enaml-syntax highlighting in PyCharm? Or maybe in some other IDE?
(I know that the package 'enaml-pygments' exists, but I have no idea how to make it work for automatic enaml-syntax highlighting in an IDE.)
I've added most of the keywords to this repo which you can import into PyCharm:
There are highlighters for a few editors in the Enaml repo. Maybe one of them can be used by PyCharm: https://github.com/nucleic/enaml/tree/master/tools
Today I found this settings menu that partially addresses the issue of adding any syntax to PyCharm:
https://blog.jetbrains.com/idea/2010/09/custom-file-types-in-intellij-idea/
Unfortunately it captures only the most simple features of a syntax.
I'm using Atom to work with Python/Enaml code. With the language-enaml package you get syntax highlighting plus some autoextension and docstring feature for Enaml (https://atom.io/packages/language-enaml). Add in git integration and packages like build-python to run you code from the editor and you have quite a nice IDE.
https://github.com/vahndi/pycharm-enaml-keywords
Open PyCharm
Go to File,
Import Settings...,
browse to the .jar file and click OK
Select All,
OK
I am using a mac, I set up all the external libraries through PYTHONPATH, I tried setting it through Pydev interpreter as well.
It simply does not work. The odd thing is that pydev is highlighting the syntax as error, but when I run the code, it works perfectly. I am out of ideas of what the problem might be. I used to program in java with eclipse, and once you set up the external libraries all the highlighting and autocompletion is working. With pydev I have no idea what is wrong.
Here is the image of the wrong highlighting:
There's a similar problem with importing wxPython with PyDev Aptana Studio 3. Based on the research I did then, PyDev has a hard time validating new imports when a module has recently been downloaded. This means that there is just a bug in PyDev, so it would be a good idea to report it.
As for the solution, from what I've found a useful link with a ton of answers that should fix your problem. There's some detailed instructions but the fix that I found was towards the bottom someone said to try wx = wx after importing wx. This worked for me, but may not work for you so here is the link if you need a better answer:
Undefined variable from import when using wxPython in pydev
I am not a native English speaker. When I code with Python, I often make spelling mistakes and get 'NameError' Exceptions. Unit test can solve some problems but not all. Because one can hardly construct test cases which cover all logic. So I think a tool that detect such errors would help me a lot but I searched Google and cannot find it.
I believe that testing your code via a static analyzer (e.g. pylint) will help a lot.
Another hint: you can use a fancy IDE with smart auto-completion which will reduce the amount of such mistakes.
I prefer eclipse with pydev integration for python development projects.
It will solve your purpose and will show you the errors in RED before you run your program. but it requires your project to be properly configured under eclipse as below:
Configure Python interpreter after integrating PyDev with eclipse
Create / Import your project & set as python pydev project
Configure your source folder ( Give information to eclipse that which folders contains python source code under your entire project tree)
You're done & setup now for general python programming setup.
Further more you can integrate your Eclipse/Pydev project with pylint as mentioned by BasicWolf which checks your code quality & bugs on the go while you're coding.
References:
Installing PyDev under Eclise
Eclipse PyDev Integration
Troubleshooting PyDev/PyLint Integration
pylint can help here. It will report those locations as
E: 2,6: Undefined variable "foo"
for example.
You could get an IDE which helps a bit with autocompletion of names, though not in all situations. PyDev is one such IDE with autocompletion; PyCharm is another (not free).
Using autocomplete is probably your best bet to solve your problem in the long term. Even if you find a tool which attempts to correct such spelling errors, that will not solve the initial problem and will probably just cause new ones.
I want to use this PEP8 module that has name checking support...
https://github.com/posborne/pep8
However when I select this, nothing happens, NO pep8 checking happens.
Can I do anything?
Pydev version 2.4
I spent some time trying to debug this. I don't know much about PyDev nor Java, but it would seem that:
PyDev is trying to parse pep8.py using it's own Jython implementation instead of local Python's one (probably they want to make it faster this way). Jython (at least the one I have) cannot parse the "yield" keyword, that's why newer versions of pep8.py don't work.
The older versions don't work either, because PyDev requires some custom-tailored signatures of pep8.py's methods which were not marged into the main stream.
I ended up making this simple Eclipse plugin. I was able to connect latest version of pep8.py with my editors. Hope it helps someone ;)
You mean that you've enabled pep8 checking and you're setting that file in window > preferences > code analysis > pep8.py > location of pep8.py?
If that is the case, it could be because of some bugfixes which were done in the internal version of PyDev (to work under the PyDev internal Jython 2.1 version)... those were already contributed back to pep8.py, but maybe were not integrated in that branch? (i.e.: pull request: https://github.com/jcrocholl/pep8/pull/49)
If that custom module did something else that doesn't work on Jython 2.1, that'd explain it too.
If that's the case, the proper solution would be fixing that pep8 module to work under Jython 2.1...
Now, if it's something else entirely... Do you have some error in your error log?
I am using PyDev/Eclipse for several monthes and I get ever and ever the same bugs with imports:
PyDev underline in red an import and say Unresolved import xxx ; Found at yyy. When I click on yyy eclispe find and open the implementation of the module.
(PyDev just inform me that it can't find the module xxx and in the same message that it can find it !)
The module xxx is in the PYTHONPATH of eclipse. When I "explore" the interpreter of the project, I can find it without any problems.
When I try to execute (from eclipse), I don't get any error and it works fine.
Sometimes, the error message will stay for several days and will disappear. Sometimes, it won't. I've tried to refresh the projects but it has not impact on that.
Somtimes, it works well on a project and I can use autocompletion and it don't work in another project (same interpreter) ...
I just can't understand what is happenning ?
So far, I have ignored these bugs because everything was fully fonctionnal but sometimes, it is a bit disturbing to have red markers "errors" when you are working.
Did you find a way to avoid these bugs in PyDev ? Is it "normal" ? Is there a way to force PyDev to "refresh" ?
Thank you.
References :
python 2.4.4 (built from sources)
PyDev v 1.6.0 2010071813
This can happen if new modules are not cached by PyDev. For example, on my new laptop I first set up PyDev/Eclipse and later installed the Django package. That's why Django imports were marked as unresolved. You can refresh it using Pydev > Interpreter - Python > Libraries > Apply. Select the interpreter you want to "restore" (they could have chosen a better word) and click OK. PyDev will then reparse all installed modules.
I'm using the nightly version of PyDev, but any 1.6.x version should work correctly. If that doesn't help, remove the interpreter configure and create a new one.
You can try refreshing your PYTHONPATH in Preferences > Pydev > Interpreter - Python and selecting AutoConfig for your interpreter, then manually choosing the libraries for your PYTHONPATH.
This is pretty radical solution though. Making an insignificant change (like adding a space) and saving file should work in most cases. If not, you can also try temporary delete and then re-add the imports in file that is causing problems.
Just in case anyone else runs into this thread but above answers don't solve the issue, make sure that your script does not have the same name as the library that you are trying to import.