I have discovered an issue with python-mode in emacs. I generally c++ develop and seldom do python.
I have recently discovered this issue:
I emacs –Q
I open a python file
It contains:
import re as myre
Var = [
%
The % represents the cursor location. Then, at that location I try to tab and get this error:
Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
python-indent-context()
python-indent--calculate-indentation()
python-indent-calculate-indentation(nil)
python-indent-line(nil)
python-indent-line-function()
indent-for-tab-command(nil)
call-interactively(indent-for-tab-command nil nil)
command-execute(indent-for-tab-command)
I have not developed in python for a month or so but I cannot remember this being an issue.
I am using emacs 24.5.1 windows 7 64, python 2.7.3 and – of course - -Q so no configuration.
Now, I try to apply python-mode 6.2.1 by running this
Emacs –Q
In scratch
(setq load-path (append load-path (list "~/.emacs.d/python-mode.el-6.2.1")))
(require 'python-mode)
I open up a python file (the same as above) then I CAN indent. This is all well and good, so if I load python-mode 6.2.1 el file in my NORMAL configuration this solve the issue, BUT now with the new 6.2.1 I do not get the same theme coloring as before (it is now bland and variable are just the same colour as other text, rather than standing out. Also which-function-mode seems to be broke (again) and developing in python is sluggish (when you open a large file) - I remember python-mode and which-function-mode not being friendly with each other in 24.3, but it was solved with STOCK el 24.5
For me, unfortunately, 6.2.1 solves one issue but creates others - INCLUDING regressions.
If, instead, I can just have the patch that solves the indentation issue, that would be great.
Thank you.
python-mode.el's py-variable-name-face inherits default face. Use M-x customize-face RET ...
Please file bugs reports at
https://gitlab.com/python-mode-devs/python-mode/issues
or
https://bugs.launchpad.net/python-mode
Related
I am new to emacs & trying to use it for python programming. I have installed elpy & everything is working fine except one thing - I am getting lot of warnings, errors like E401, E402, E501 etc. All are in scary red colors.
After researching little bit, it looks like these errors are coming from flake8. So I configure flake8 as follows:
In ~/.config/flake8,
[flake8]
ignore=E201,E203,E211,E221,E272,E251,E211,E222,E226,E228,E241,E301,E302,E401,E402,E501,E701,F401
max-line-length=160
exclude=tests/*
max-complexity=10
But still those errors are coming. I also configured same way PEP8 & pycodestyle in .config/. But no progress. I tried to put setup.cfg & tox.ini in project root, but that also didn't work.
For more information, there is similar ticket in sublime flake8 repo.
https://github.com/SublimeLinter/SublimeLinter-flake8/issues/24
Please let me know if there is any working solution or emacs hacks.
Following are some details
Os - Linux Mint 17.3 Rosa
flake8 version - 3.0.0b1 (pyflakes: 1.2.3, pycodestyle: 2.0.0, mccabe: 0.5.0)
GNU Emacs 24.3.1
Thanks in advance.
Anyone trying to get this running for flake8>=4.0.0, that's because support for global config file is no longer present, see https://flake8.pycqa.org/en/latest/release-notes/4.0.0.html#backwards-incompatible-changes.
Try restarting emacs. For me it picks up ~/.config/flake8 when emacs is started, but doesn't pick it up between runs.
I have just happily configured emacs with autocompletion via jedi and syntax check via flycheck and virtualenvs created within bootstrap. It all seems to work.
I'd like to add the ability to use flycheck-pylint (to get errors in import) but I'm not able to make it work. Even if I change the virtualenv by hand (M-x: pyvenv-activate RET path-to-my-venv) I still see lots of import errors that come from a wrong virtualenv used.
My current initialization code:
(require 'pyvenv)
(add-hook 'after-init-hook #'global-flycheck-mode)
(defun set-flake8-executable ()
(pyvenv-activate (get-current-buffer-venv))
(flycheck-set-checker-executable (quote python-flake8)
(get-current-buffer-flake8)))
where "get-current-buffer-venv" and "get-current-buffer-flake8" are functions that implement my specific setup and are working correctly.
How can I change the interpreter used?
Thanks to an answer from Lunaryorn on github i realized there is also a flycheck-set-pylint-executable. Now all is working correctly whith the following configuration:
(defun set-flychecker-executables ()
"Configure virtualenv for flake8 and lint."
(when (get-current-buffer-flake8)
(flycheck-set-checker-executable (quote python-flake8)
(get-current-buffer-flake8)))
(when (get-current-buffer-pylint)
(flycheck-set-checker-executable (quote python-pylint)
(get-current-buffer-pylint))))
(add-hook 'flycheck-before-syntax-check-hook
#'set-flychecker-executables 'local)
Poking at the problem today, I found another solution (which works with current version of flycheck, as of Jun 2020).
Just create .dir-locals.el with appropriate settings for given project. Like:
((python-mode
(flycheck-python-flake8-executable . "/home/marcin/.virtualenvs/adgv/bin/python")
(flycheck-python-pylint-executable . "/home/marcin/.virtualenvs/adgv/bin/python")))
(creating the file with M-x add-dir-local-variable works too, but remember to add double quotes around the command)
We've been long-time fans of pylint. Its static analysis has become a critical part of all our python projects and has saved tons of time chasing obscure bugs. But after upgrading from 1.3 -> 1.4, almost all compiled c extensions result in E1101(no-member) errors.
Projects that previously run perfectly clean through pylint 1.3 now complain about almost every C extension member with E1101. We've been forced to disable E1101 errors, but this materially detracts from the usefulness of pylint.
For example, this perfectly valid use of the lxml package
r"""valid.py: demonstrate pylint 1.4 error"""
from lxml import etree
print etree.Element('mydoc')
Run this through pylint, and it reports:
$ pylint -rn valid.py
No config file found, using default configuration
************* Module valid
E: 3, 6: Module 'lxml.etree' has no 'Element' member (no-member)
But it is perfectly valid:
$ python valid.py
<Element mydoc at 7fddf67b1ba8>
Here's where it gets really weird. A very small handful of C extensions seem to work just fine through pylint, e.g.:
r"""valid2.py: this one works fine"""
import sqlite3
print sqlite3.version
$ pylint -rn valid2.py
No config file found, using default configuration
My question is, has anyone else witnessed this? And if so, would you be willing to share your workaround/solution?
We've experimented with trying to create plugins to suppress these warnings
(http://docs.pylint.org/plugins.html#enter-plugin), but we're having difficulty making heads or tails of the docs -- and the astroid base class is uber-complex and has defied our attempts to grok it.
For real bonus points (and our eternal gratitude) we'd love to understand what changed in pylint. We'd be happy to fix the code (or at least publish a best practice document for C extension authors) that would satisfy pylint.
Platform details
$ pylint --version
No config file found, using default configuration
pylint 1.4.0,
astroid 1.3.2, common 0.63.2
Python 2.7.5 (default, Jul 1 2013, 18:09:11)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]
Shortly after posting my question, I found the answer. The change was in fact done on purpose as a security measure. Pylint imports modules to effectively identify valid methods and attributes. It was decided that importing c extensions that are not part of the python stdlib is a security risk and could introduce malicious code.
This was done in the release of Astroid 1.3.1 https://mail.python.org/pipermail/code-quality/2014-November/000394.html
Only C extensions from trusted sources (the standard library) are
loaded into the examining Python process to build an AST from the live
module.
There are four solutions if you want to use pylint on projects that import non-stdlib c extensions.
1) Disable safety using the --unsafe-load-any-extension=y command line option. This feature is undocumented and classified as a hidden option (https://mail.python.org/pipermail/code-quality/2014-November/000439.html).
2) Disable safety using the pylint.rc setting unsafe-load-any-extensions=yes. This is recommended over option 1 and includes full documentation in the default pylint.rc file (created with --generate-rcfile).
3) Specifically list packages or modules names that you trust to be loaded by pylint in the pylint.rc file using the extension-pkg-whitelist= option.
4) Create a plugin to manipulate the AST (I have no idea how to effect this -- but it's regularly discussed on on the pylint mailing list).
We opted for Option 3. We added the following line to our project pylint.rc file:
extension-pkg-whitelist=lxml
#user590028, thanks a lot for your answer! I just ran into this same problem with the libraries win32api, win32evtlog, win32file, win32gui, and win32process, and your solution worked.
I used another method I think is worth posting here, which is to call pylint and pass the whitelisted packages as a parameter:
pylint --extension-pkg-whitelist=win32api,win32evtlog,win32file,win32gui,win32process myfile.py
For those of you using VS Code, it's a bit tricky to find where to put the command as I couldn't find my executable.
In VS Code;
click on File > Preferences > Settings.
Scroll down to "Python Configurations" in the left window
scroll down to "Python Linting: Mypy Args" in the right window
click on "Edit in settings.json" link
edit the json to include:
"--extension-pkg-whitelist="
I had to do all this because PyLint isn't executable from my Windows command line...
If you're using VS Code for Mac, this is what you need to do in order to edit the settings.json file:
Click on Code (i.e. the Visual Studio Code tab which is on the left of the 'File' tab) -> Preferences - > Settings
Scroll down to Extensions and click on Python in the list.
Click on any of the Edit in settings.json links. This opens up settings.json for editing.
Add the line "python.linting.pylintArgs": ["----extension-pkg-whitelist=1xml"].
I'm using Emacs 24.4.1 on OSX (installed with Homebrew), with the built in python.el, and Python 3 (also installed with Homebrew), along with IPython 2.3.0. I have this in my .emacs:
(setq
python-shell-interpreter "/usr/local/bin/ipython3"
python-shell-prompt-regexp "In \\[[0-9]+\\]: "
python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
python-shell-completion-setup-code
"from IPython.core.completerlib import module_completion"
python-shell-completion-module-string-code
"';'.join(module_completion('''%s'''))\n"
python-shell-completion-string-code
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
Everything works correctly when I invoke M-x run-python, except one thing: the <tab>-completion of module names doesn't work in the REPL (it says "No match" in the *Messages* buffer). I'm pretty sure it was working with Emacs 24.3, which I upgraded very recently, but I'm not 100% sure. Anyone has an idea what might cause this, or how I could try to debug it?
You might like to try and remove most of your settings above, since Emacs-24.4 should support IPython pretty much out of the box.
I'm using Windows 7, latest windows update etc.
Recently I decided to start using the minted package in LaTeX. For this, I was redirected to install pygments, which I installed through pip:
I'll run you through quickly what I did (to make sure I didn't do anything wrong):
Installed python 3.4 from this page.
easy_install pip in the windows command prompt.
pip install pygments in command prompt.
I've added the %PYTHONPATH% as well as %PYTHONPATH%\Scripts (which is where pygments operates from I've been led to believe), to the system PATH environment.
I'm typing source code from R into LaTeX, but the releases I could find do not offer the capital R syntax. If I use LaTeX code as such:
\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{R} % Capital R won't function as intended with recent release
c(1,2,5,7,8)
\end{minted}
\end{document}
It won't recognise the capital R argument in minted.
Now, the lower case \begin{minted}{r} works just fine, so I've looked around on how to fix this, and found this answer.
Here it is suggested to adjust the math.py lexer (at %PYTHONPATH%\Lib\site-packages\pygments\lexers) to add an alias 'R' to current aliases. It also suggests to run _mapping.py in the same directory (%PYTHONPATH%\Lib\site-packages\pygments\lexers). Running _mapping.py has two effects:
It completely empties the file, the file size is reduced to 0 bytes and editing it with IDLE confirms an empty file.
It also yields this error message.
Not running _mapping.py won't apply my new alias to pygments. Running it, breaks pygments entirely. The error code is shown below.
I ran the LaTeX code again (with changes and _mapping.py also run), with pdfLaTeX, with arguments: $synctexoption, --enable-write18, -interaction=nonstopmode and $fullname (also in this order), I get the following error in LaTeX:
LaTeX Error: File `temp.out.pyg' not found.
Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: out.pyg)
Enter file name:
! Emergency stop.
<read *>
l.64 \end{minted}^^M
After running _mapping.py on a clean installation even (no changes made to any lexer or anything), I also run into the same problem. I'm uncertain whether the initial changes I made, changed registry values and whatnot, but I'm having serious issues applying this simple change to the pygments lexer.