30

My Pylint doesn't point to the virtualenv Python interpreter. Here is the output that I get when I run pylint --version:

pylint --version

Output:

   pylint 0.21.1,
   astng 0.20.1, common 0.50.3
   Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45)
   [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]

In virtualenv, I have Python 2.7 installed. How can I make Pylint point to the 2.7 version in the virtual environment?

8
  • What's the output of which pylint and where is your venv? Commented Jul 29, 2013 at 11:43
  • @kroolik it is /usr/bin/pylint and my virtualenv is ~/virtenvs/my_env/bin/python Commented Jul 29, 2013 at 11:45
  • Did you install pylint with your venv enabled? Once you have installed pylint using pip install pylint with your venv active, which pylint should point to ~/virtenvs/my_envs/bin/pylint Commented Jul 29, 2013 at 11:47
  • Yes I did installed pylint with my virtenv enabled. Commented Jul 29, 2013 at 11:47
  • Have you got ~/virtenvs/my_env/bin/pylint file? Commented Jul 29, 2013 at 11:48

11 Answers 11

50

A cheap trick is to run (the global) Pylint using the virtualenv Python instance.

You can do this using python $(which pylint) instead of just pylint. In Z shell (executable zsh), you can also do python =pylint.

Sign up to request clarification or add additional context in comments.

11 Comments

Great, thanks. Since I use Python 3, I had to do $(which pylint3).
@NoufalIbrahim I just love you so much for this =command hack. Using zsh for quite a while now, but I never stumbled upon that one.
You're welcome luke. It's just a more succinct way of writing $(which foo).
Unfortunately it doesn't seem to work in Vim with ALE plugin.
This didn't work for me as global packages (here pylint) are not available in venv by default. Running python /usr/bin/pylint gives importlib.metadata.PackageNotFoundError: pylint inside venv
|
24

I am fairly sure that you need to install Pylint under your virtual environment and then run that instance of it.

Make life easier:

I would suggest that anybody working a lot in virtual environments create a batch file, (in a known location or on the path), or Bash script with something like the following called something like getlint.bat:

pip install pylint

Invoking this after activating the virtual environment will install Pylint into that virtual environment. If you are likely to be offline or have a poor Internet connection you can, once when you have a good Internet connection, (possibly once for each of Python 2 and 3):

mkdir C:\Some\Directory\You\Will\Leave\Alone
pip download --dest=C:\Some\Directory\You\Will\Leave\Alone pylint

Which will download Pylint and its dependencies to C:\Some\Directory\You\Will\Leave\Alone, and you can modify getlint.bat to read:

pip install pylint --find-links=C:\Some\Directory\You\Will\Leave\Alone

It will then use the pre-downloaded versions.

4 Comments

Steve is correct, I'll add that ^uninstalling^ it from your global python is also a good idea. That way you ensure you're running the correct pylint.
There goes a sad +1 since this is a real pain when using multiple (10+) virtualenvs.
@mpiskore - I have expanded the answer a little to make life easier.
I would like to add that if you created the virtualenv with --system-site-packages (e.g. because you use it to run your editor and don't want to debug issues with missing system packages), you may need to pass --force-reinstall to Pip. Alternatively --ignore-installed might also work, but I did not try that so I'm not sure and I cannot comment on what is better.
9

Noufal Ibrahim's answer works if you execute Pylint manually.

If you execute Pylint from you editor/IDE, you need to configure the plugin correctly.

It can get tricky. This may be considered a bug of each IDE/plugin, but that's how it is.

Modifying /usr/bin/pylint to write #!/usr/bin/env python as suggested in another answer fixes this for every use of Pylint (manual use, or any editor integration).

However, at least in Debian, using #!/usr/bin/python is a design choice, not a bug. See here for the rationale.

To avoid modifying that system file, one can create a copy of /usr/bin/pylint in /usr/local/bin:

cp /usr/bin/pylint /usr/local/bin/pylint
vi usr/local/bin/pylint # Edit the file to use /usr/bin/env python

This won't be broken by a Pylint update, but still infringes Debian's "strongly preferred choice".

This method requires root privileges. An unprivileged user may create an alias

alias pylint='/usr/bin/env python $(which pylint)'.

I always develop in virtualenv and I set up a postmkvirtualenv hook to install Pylint and flake8 automatically when creating a virtualenv, so I don't use the versions distributed by Debian anymore.

1 Comment

See here for the "no use env" rationale: debian.org/doc/packaging-manuals/python-policy/…
6

I ran into this problem, too. My solution was simply to edit the Pylint program's shebang, like so... (your path to Pylint may be different than mine, though)

sudo vim /usr/bin/pylint

Replacing:

#!/usr/bin/python

With:

#!/usr/bin/env python

5 Comments

Altering system programs like this for one user is not a good practice.
@NoufalIbrahim Isn't this more like a bug from pylint developers that affects all of a system's users?
Nope. It's a decision by the managers of the distro to make sure that the pylint will work as advertised and is independent of your local PATH settings.
@NoufalIbrahim Oh... is that so? Very educational, good man. That's a big deal to chew on. Can I ask you to submit a better answer / workaround? I get the feeling that you have a novel solution using local PATH settings.
@starlocke I don't understand what you're asking for.
5

The issue has been solved in chat (link in comments).

The problem lay in using sudo yum install pylint, because it installed Pylint in the global environment. The solution was to use the following command:

pip install -i http://f.pypi.python.org/simple pylint

Note the -i usage as the regular index seemed to be broken for the asker.

Comments

2

If for some reason you need to keep Pylint in the global space instead of your virtual environment, you can use the recommendation in here: Pylint + VirtualEnv.

It basically says to configure your Pylint using the init-hook and encoding version of a Python program that will use the global Pylint and load the rest of the environment.

Comments

2

When you're using Pipenv / virtualenv, install Pylint inside the virtualenv:

pipenv install --dev pylint

Or, if you don't use Pipenv, install it with pip after you activated your virtualenv:

# Activate virtualenv, e.g., `. env/bin/activate`
pip install pylint

Comments

1

You can get there by calling the target Python interpreter:

./env/bin/python -m pylint ...

# Or in an already active environment
python -m pylint ...

Comments

0

For pylint installed globally, it can not recognize the packages installed inside the virtual environment, even if we are inside the virtual env. First, we can install pylint-venv in the global level (outside the virtual env):

pip install pylint-venv

Then inside the pyproject.toml file for this project, we can configure a init-hook for pylint:

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

Tested on pylint 2.17, works well for me.

Comments

0

I'm using the Syntastic and Pylint combination, and since I have many different virtualenvs that I can work on at any given time, I've created a wrapper over the virtualenv command that, among some other things, installs Pylint after all the requirements.

That way, whenever I activate a virtualenv, I'll get its own Pylint version.

Comments

0

I ran into the same problem just today. Continuing on ThorSummoner's answer, when using Pylint with pylint-django inside of a virtual environment, such as Pipenv, make sure to call Pylint using the target python interpreter (python -m pylint).

It is a good approach, which will work locally and on your CI as well is to write-down the lint command in the script section of your Pipfile:

[scripts]
lint = "python -m pylint [--options] all-my-modules-names..."

Then calling Pylint is as easy as:

pipenv run lint

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.