Code Monkey home page Code Monkey logo

pylint-venv's Introduction

CI and release pipeline PyPI PyPI - Python Version PyPI - License

pylint-venv

Pylint does not respect the currently activated virtualenv if it is not installed in every virtual environment individually. This module provides a Pylint init-hook to use the same Pylint installation with different virtual environments.

Installation

pip install pylint-venv

Add the hook to your Pylint configuration. See the section below corresponding to the type of configuration file you use.

Configure with pyproject.toml

Add the following to your pyproject.toml:

[tool.pylint.MAIN]
init-hook = """
try: import pylint_venv
except ImportError: pass
else: pylint_venv.inithook()
"""

Configure with .pylintrc

Add the following to your .pylintrc:

[MAIN]
init-hook=
    try: import pylint_venv
    except ImportError: pass
    else: pylint_venv.inithook()

If you add this to your ~/.pylintrc in your home directory, it will be applied to all projects by default.

Usage

The hook will then be used automatically if

  • a virtualenv without pylint is active,
  • or a Conda environment without pylint is active,
  • or no environment is active but your CWD contains virtualenv directory.

Anything listed in the PYLINT_VENV_PATH environment variable is considered a virtualenv directory. The default, if the variable is unset, is .venv. Use a colon (:) as path separator. Example for checking directories .venv and .virtualenv:

PYLINT_VENV_PATH=.venv:.virtualenv

You can also call the hook via a command line argument:

$ pylint --init-hook="import pylint_venv; pylint_venv.inithook()"

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:

$ pylint --init-hook="import pylint_venv; pylint_venv.inithook(force_venv_activation=True)"

This will try to automatically detect virtualenv and activate it.

Troubleshooting

General

pylint_venv fails to import

Most likely pylint-venv is not installed in the same virtual environment as pylint. Either make sure to ensure pylint-venv into the same virtual environment as pylint, or add the appropriate path in the init hook:

import sys
sys.path.append("/path/to/installation/folder/of/pylint_venv")
pylint_venv breaks parsing with tools

When tools call pylint with -f json, an extra line may break the parser, as the output is no longer valid json. To avoid printing "using venv ...", pass quiet=True to inithook

$ pylint -f json --init-hook="import pylint_venv; pylint_venv.inithook(quiet=True)"
Virtual environment does not get used (installed modules are reported as 'unable to import')

Most likely the virtual environment does not get activated because pylint itself runs in a virtual environment. You can force the activation of the virtual environment with the force_venv_activation=True flag to the pylint_venv.inithook function.

Homebrew

Homebrew installs pylint into a separate virtual environment, thus you will need to set the force_venv_activation=True flag. This also means, that pylint_venv will be in a different search path and you must add the proper path to sys.path. You can use the following configuration adjusted to your Python version:

[MAIN]
init-hook=
    import sys
    sys.path.append("/usr/local/lib/python3.8/site-packages")
    try: import pylint_venv
    except ImportError: pass
    else: pylint_venv.inithook(force_venv_activation=True)

pylint-venv's People

Contributors

alfaix avatar bnavigator avatar ccordoba12 avatar exagone313 avatar flimm avatar jgosmann avatar jmfederico avatar mgorny avatar sscherfke avatar wadkar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pylint-venv's Issues

Add support for more locations

This plugin only works for virtualenvs created on .venv.

It could also work on other locations, such as .pyenv and .virtualenv.

How to configure pylint-venv in VS Code?

Hello developers
Thanks for this excellent library. pylint-venv works all good in command line. I was wondering if there is a way to configure this in VS Code as well.

I have configuring VSCode user settings using pylintArgs something like this but this does not seem to work.

"python.linting.pylintArgs": [
    "--init-hook=import pylint_venv; pylint_venv.inithook()",
],

and

"python.linting.pylintArgs": [
    "--init-hook",
    "import pylint_venv; pylint_venv.inithook()",
],

Any help on this? Thanks

EDIT: Just open VSCode in virtualenv itself. No need to configure anything. And pylint-venv works like a charm.

Pylintrc init-hook documentation update needed

Hi;

Getting the system-installed pylint to import modules in my virtualenv-based project has been a pain; glad I found this module!

That said, it wasn't drop-dead easy for me to configure pylint correctly to use it! :-(

Environment: MacOS Catalina (10.15.5), Brew, python 3.8.3, pylint 2.5.3, asteroid 2.4.2

The instructions at https://pypi.org/project/pylint-venv/ said to add this to ~/.pylintrc:

init-hook=
    try: import pylint_venv
    except ImportError: pass
    else: pylint_venv.inithook()

This didn't seem to have any effect, so after much mucking about I came up with this:

init-hook=
    import sys
    sys.path.append("/usr/local/lib/python3.8/site-packages")
    import pylint_venv
    pylint_venv.inithook(force_venv_activation=True)

I'm still not happy with my efforts, since who puts python version numbers in their code? I mean, really? :-)

If you could change the documentation a bit to make it easier for the next folks, that'd be great!

Thanks

config for pyproject.toml

This is what is working for me:

[tool.pylint.main]
init-hook ="""
try:
  import pylint_venv
except ImportError:
  pass
else:
  pylint_venv.inithook()
"""

The config in readme does not work due to parsing issues. Test on pylint 2.17.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.