How can I use configured virtualenv and flake8 settings file (setup.cfg in root of project), for flycheck in emacs?
There are a number of plugins for working with virtualenvs. For example, with pyvenv installed I can use the pyvenv-workon command to select a virtualenv from $WORKON_HOME.
virtualenvwrapper and python-environment provide similar support, and all three are available on MELPA.
Flycheck has an option flycheck-flake8rc:
Configuration file for `python-flake8'.
If you have per-project configuration files, it might be easiest to create .dir-locals.el files in the root of each project that sets flycheck-flake8rc to the appropriate value, e.g.
((python-mode
(flycheck-flake8rc . "/path/to/setup.cfg")))
I would advise not tracking .dir-locals.el in whatever version control system you are using, though of course that is up to you.
I spent some time to find right solution and decided to implement my own:
flycheck-local-flake8
This is plugin for flycheck-flake8 checker, simply uses flake8 from required virtualenv and setup.cfg from root of the python project.
Related
I was trying to understand the purpose of use_develop and from the docs, I found this:
Install the current package in development mode with develop mode. For pip this uses -e option, so should be avoided if you’ve specified a custom install_command that does not support -e.
I don't understand what "development mode" means. Is this a python concept or it's specific to tox? Either way what does it mean?
development mode or editable installs is a Python concept, or even more specific a Python packaging concept.
Usually, when you package a Python application or library, the source files are packaged into a "container", either a wheel or a source distribution.
This is a good thing to distribute a package, but not for developing, as then the source files are no longer accessible.
editable installs is a concept, that instead of "moving/copying" the files into the package container, the files are just symlinked (at least this is one way).
So when you edit the source files, also the package is updated immediately.
For tox this also means that the files in the root of the source tree are importable by Python, not only the one in the package.
This might be comfortable, but there is one huge caveat. If you misconfigure the packaging setup, maybe the tests ran by tox are green, but it is entirely possible that you forget to include the source files in the package you deliver to your users.
The venv module (shipped with Python 3.3 or later), and virtualenv, still widely in use, allow to install a project's dependencies not to the system-wide Python installation, but to a directory specific to that project.
One of the subdirectories of such a "virtual environment" contains a copy of the Python interpreter as well as "activate" and "deactivate" scripts - but this subdirectory is called Scripts on Windows, and bin on all other systems.
This is somewhat surprising. Why did they special-case Windows?
(Neither PEP 405, nor the venv or virtualenv sources (or docs) contain any explanation - a commit message in virtualenv refers to "convention")
"I think the commit message is the best you'll get. Everything else will be pure speculation." (Bryan's comment, refering to the commit message in virtualenv)
Most ms-windows programs have a GUI which is started by an icon or menu-entry. So there is no need for a standardized location for binaries (which is then put in the $PATH) such as UNIX has. Also, the name bin wouldn't mean anything like it does to UNIX users.
Additionaly, ms-windows only has a very primitive package management (if you can even call it package management), so applications tend to be installed in their own directory tree where they won't interfere with each other.
How do I switch betwen Python 2 and 3 when using Flycheck with flake8 in Emacs?
Having installed Flycheck for Emacs, I then install a syntax-checker like flake8.
Installing flake8 using pip install flake8 makes Flycheck check Python 2 syntax.
However, using pip3 install flake8 makes Flycheck check the syntax for Python 3.
How can I switch between the modes for Python 2 and 3 in Emacs 24?
If you use virtual environments, either using anaconda, miniconda or virtualenv, then I recommend that you use pyvenv.el. It requires a tiny bit of configuration which I may be able to help you out with if you need but it is generally pretty easy to setup. I use it to switch between different python versions and different virtual environments.
You should also be able to simply place a variable in your .dir-locals.el file. This file sets emacs variables based on the directory that you're in. If you want to set your python-interpetter based on the project or directory that you're in you can simply create a file called .dir-locals.el in the folder with the project files that you want to edit. In that file you should then have
((nil . ((python-shell-interpreter . "python3"))))
or
((nil . ((python-shell-interpreter . "python"))))
flycheck should then switch for you. Let me know if you have any questions. Also maybe take a look at this from the creator of flycheck
Trying to install nosetests as per the learnpythonthehardway tutorial, I'm having problems. Any clues on what I should try next?
$ easy_install nose
Searching for nose
Best match: nose 1.1.2
Processing nose-1.1.2-py2.6.egg
nose 1.1.2 is already the active version in easy-install.pth
Installing nosetests-2.6 script to /usr/local/bin
error: /usr/local/bin/nosetests-2.6: Permission denied`
One question about installing that I have: if I have something saved in a random location on my computer, can it be imported into a python script regardless of where it is? So if I execute runthis.py that's in a folder called "projects", and I have from setup tools import setup as the first line of the program, does setup tools have to be anywhere in particular (such as the "projects" folder) for python to find it?
Are you able to use sudo?
If so, simply use sudo easy_install nose to install as root.
If not, you'll need to install somewhere you can write to, not the default location which you don't have permission to modify. This can be done easily in the traditional way, or using virtualenv which can a bit trickier to get set up initially.
As for the second question, no, python will only find things that are in directories found in sys.path, which is set to the contents of the PYTHONPATH environment variable plus the installed python's own library directories by default.
It is often (highly!) advisable to set up your own "local" repository of packages, for whatever language system (be it Python or otherwise) that you are using. Leave the "system installed" packages, whatever they might be, completely alone ... in case some uber-important system tool (the package-manager, anyone?) might also be using them and might be dependent on them.
The means of doing this vary from language to language, but they'll be documented somewhere all the same. You might even find that the "distro" that you're using has already anticipated this requirement and has set-aside some agreed upon location, e.g. "/usr/local/..." just for your own personal use.
I'm developing a Python utility module to help with file downloads, archives, etc. I have a project set up in a virtual environment along with my unit tests. When I want to use this module on the same computer (essentially as "Production"), I move the files to the mymodule directory in the ~/dev/modules/mymodule
I keep all 3rd-party modules under ~/dev/modules/contrib. This contrib path is on my PYTHONPATH, but mymodule is NOT because I've noticed that if mymodule is on my PYTHONPATH, my unit tests cannot distinguish between the "Development" version and the "Production" version. But now if I want to use this common utility module, I have to manually add it to the PYTHONPATH.
This works, but I'm sure there's a better, more automated way.
What is the best way to have a Development and Production module on the same computer? For instance, is there a way to set PYTHONPATH dynamically?
You can add/modify python paths at sys.path, just make sure that the first path is the current directory ".", because some third-party modules rely on importing from the directory of the current module.
More information on python paths:
http://djangotricks.blogspot.com/2008/09/note-on-python-paths.html
I'm guessing by virtual environment you mean the virtualenv package?
http://pypi.python.org/pypi/virtualenv
What I'd try (and apologies if I've not understood the question right) is:
Keep the source somewhere that isn't referenced by PYTHONPATH (e.g. ~/projects/myproject)
Write a simple setuptools or distutils script for installing it (see Python distutils - does anyone know how to use it?)
Use the virtualenv package to create a dev virtual environment with the --no-site-packages option - this way your "dev" version won't see any packages installed in the default python installation.
(Also make sure your PYTHONPATH doesn't have any of your source directories)
Then, for testing:
Activate dev virtual environment
Run install script, (usually something like python setup.py build install). Your package ends up in /path/to/dev_virtualenv/lib/python2.x/site-packages/
Test, break, fix, repeat
And, for production:
Make sure dev virtualenv isn't activated
Run install script
All good to go, the "dev" version is hidden away in a virtual environment that production can't see...
...And there's no (direct) messing around with PYTHONPATH
That said, I write this with the confidence of someone who's not actually tried setting using virtualenv in anger and the hope I've vaguely understood your question... ;)
You could set the PYTHONPATH as a global environment variable pointing to your Production code, and then in any shell in which you want to use the Development code, change the PYTHONPATH to point to that code.
(Is that too simplistic? Have I missed something?)