Skip to content

Commit

Permalink
Merge pull request #8 from jgosmann/pyversion
Browse files Browse the repository at this point in the history
Allow activation of venv with version mismatch
  • Loading branch information
jgosmann authored Aug 17, 2020
2 parents 91ce5df + 710b9c2 commit c613ff3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.egg-info/
*.pyc
.DS_Store
.vscode
MANIFEST
build
dist
24 changes: 18 additions & 6 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [Unreleased]

### Fixed

- Activate virtual environment even if its Python version does not match the
Python version used by pylint. Note that Python packages in the virtual
environment incombatible with pylint's Python version will not work.

## [2.1.0] - 2020-03-22

### Added

- Support for using pylint installed in a virtual environment.
See `force_venv_activation` parameter on `inithook` method.
See `force_venv_activation` parameter on `inithook` method.

## [2.0.0] - 2019-10-19

### Added

- Support for Conda and PyPy
- Documentation improvements

### Removed
- Support for Python 2.7.

- Support for Python 2.7.

## [1.1.0] - 2019-03-26

### Added
- Compatibility with Python 3 venv module.

- Compatibility with Python 3 venv module.

## [1.0.0] - 2015-03-01

### Added
- Initial release of inithook for pylint to activate virtual env.

- Initial release of inithook for pylint to activate virtual env.

[Unreleased]: https://github.com/jgosmann/pylint-venv/compare/v2.0.0...HEAD
[unreleased]: https://github.com/jgosmann/pylint-venv/compare/v2.0.0...HEAD
[2.1.0]: https://github.com/jgosmann/pylint-venv/compare/v2.0.0...v2.1.0
[2.0.0]: https://github.com/jgosmann/pylint-venv/compare/v1.1.0...v2.0.0
[1.1.0]: https://github.com/jgosmann/pylint-venv/compare/v1.0.0...v1.1.0
Expand Down
18 changes: 16 additions & 2 deletions pylint_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import os
import platform
import re
import site
import sys

Expand All @@ -46,6 +47,10 @@
IS_PYPY = platform.python_implementation() == "PyPy"


class IncompatibleVenvError(Exception):
pass


def is_venv():
"""Return *True* if a virtual environment is currently active."""
is_conda_env = getattr(sys, "base_prefix", sys.prefix) != sys.prefix
Expand Down Expand Up @@ -76,8 +81,17 @@ def activate_venv(venv):
elif IS_WIN:
site_packages = os.path.join(venv, "Lib", "site-packages")
else:
pyver = f"python{sys.version_info[0]}.{sys.version_info[1]}"
site_packages = os.path.join(venv, "lib", pyver, "site-packages")
lib_dir = os.path.join(venv, "lib")
python_dirs = [d for d in os.listdir(lib_dir) if re.match(r"python\d+.\d+", d)]
if len(python_dirs) == 0:
raise IncompatibleVenvError(
f"The virtual environment {venv!r} is missing a lib/pythonX.Y directory."
)
if len(python_dirs) > 1:
raise IncompatibleVenvError(
f"The virtual environment {venv!r} has multiple lib/pythonX.Y directories."
)
site_packages = os.path.join(lib_dir, python_dirs[0], "site-packages")

prev = set(sys.path)
site.addsitedir(site_packages)
Expand Down

0 comments on commit c613ff3

Please sign in to comment.