Skip to content

Commit

Permalink
Merge pull request #4 from wadkar/master
Browse files Browse the repository at this point in the history
Make pylint-venv work if pylint is installed in a different environment
  • Loading branch information
jgosmann authored Mar 22, 2020
2 parents a74e893 + fa11f07 commit 5cc81ed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.1.0] - 2020-03-04
### Added
- Support for using pylint installed in a virtual environment.
See `force_venv_activation` parameter on `inithook` method.

## [2.0.0] - 2019-10-19
### Added
Expand Down
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,15 @@ This way you can also explicitly set an environment to be used:
$ pylint --init-hook="import pylint_venv; pylint_venv.inithook('$(pwd)/env')"
If ``pylint`` itself is installed in a virtualenv, then you can ignore it by passing
``force_venv_activation=True`` to force the activation of a different virtualenv:

.. code:: console
$ pylint --init-hook="import pylint_venv; pylint_venv.inithook(force_venv_activation=True)"
This will try to automatically detect virtualenv and activate it.

.. _Pylint: http://www.pylint.org/
.. _virtualenv: https://virtualenv.pypa.io/en/latest/
32 changes: 13 additions & 19 deletions pylint_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
pylint --init-hook="import pylint_venv; pylint_venv.inithook('$(pwd)/env')"
- If Pylint itself is installed in a virtualenv, then you can ignore it by passing
``force_venv_activation=True`` to force activation of a different virtualenv::
pylint --init--hook="import pylint_venv; pylint_venv.inithook(force_venv_activation=True)"
"""

import os
Expand All @@ -42,17 +47,14 @@


def is_venv():
"""Return ``true`` if a virtual environment is active and Pylint is
installed in it.
"""
"""Return *True* if a virtual environment is currently active."""
is_conda_env = getattr(sys, "base_prefix", sys.prefix) != sys.prefix
is_virtualenv = hasattr(sys, "real_prefix")
return is_conda_env or is_virtualenv


def detect_venv():
# Check for a virtualenv or Conda env
"""Check for a virtualenv or Conda env"""
for var in ["VIRTUAL_ENV", "CONDA_PREFIX"]:
venv = os.getenv(var, "")
if venv:
Expand All @@ -68,15 +70,7 @@ def detect_venv():


def activate_venv(venv):
"""Check for an active venv and add its paths to Pylint.
Activate the virtual environment from:
- The *venv* param if one is given.
- ``VIRTUAL_ENV`` environmental variable if set.
- A ``.venv`` folder in the current working directory
"""
"""Activate the virtual environment with prefix *venv*"""
if IS_PYPY:
site_packages = os.path.join(venv, "site-packages")
elif IS_WIN:
Expand All @@ -97,13 +91,13 @@ def activate_venv(venv):
sys.path[:] = new_paths + kept_paths


def inithook(venv=None):
"""Add a virtualenv's paths to Pylint.
Use the environment *env* if set or auto-detect an active virtualenv.
def inithook(venv=None, force_venv_activation=False):
"""Add virtualenv's paths and site_packages to Pylint.
Use environment with prefix *venv* if provided else try to auto-detect an active virtualenv.
Pass *force_venv_activation=True* if Pylint itself is installed in a different virtualenv.
"""
if is_venv():
if not force_venv_activation and is_venv():
# pylint was invoked from within a venv. Nothing to do.
return

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='pylint-venv',
version='2.0.0',
version='2.1.0',
description=(
'pylint-venv provides a Pylint init-hook to use the same Pylint '
'installation with different virtual environments.'
Expand Down

0 comments on commit 5cc81ed

Please sign in to comment.