pylint 不指向 virtualenv python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17923090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:30:03  来源:igfitidea点击:

pylint doesn't point to virtualenv python

pythonvirtualenvpylint

提问by hjelpmig

I am pretty new to python and currenty I am trying to use pylint for checking code quality. I am getting a problem. My pylint doesn't point to virtualenv python interpreter. Here is the output that I get when I run pylint --version

我对 python 很陌生,目前我正在尝试使用 pylint 检查代码质量。我遇到了问题。我的 pylint 没有指向 virtualenv python 解释器。这是我运行 pylint --version 时得到的输出

 $ pylint --version
   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. Will appretiate you help if someone can point me to how to solve that.

在 virtualenv 中,我安装了 python 2.7。如果有人能指出我如何解决这个问题,我会感谢你的帮助。

采纳答案by Noufal Ibrahim

A cheap trick is to run (the global) pylint using the virtualenv python. You can do this using python $(which pylint)instead of just pylint. On zsh, you can also do python =pylint.

一个便宜的技巧是使用 virtualenv python 运行(全局)pylint。您可以使用python $(which pylint)而不仅仅是pylint. 在 zsh 上,您也可以执行python =pylint.

回答by Steve Barnes

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

我相当确定您需要在虚拟环境下安装 pylint,然后运行它的实例。

Update - 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:

我建议任何在虚拟环境中工作很多的人创建一个批处理文件(在已知位置或路径上),或使用类似以下内容的 bash 脚本,称为类似getlint.bat

pip install pylint

Invoking this afteractivating 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 & 3):

激活虚拟环境调用它会将 pylint 安装到该虚拟环境中。如果您可能处于离线状态或互联网连接状况不佳,则可以在互联网连接良好的情况下进行一次(可能对于 python 2 和 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\Aloneand you can modify getlint.batto read:

它将下载 pylint 及其依赖项C:\Some\Directory\You\Will\Leave\Alone,您可以修改getlint.bat为:

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

It will then use the pre-downloaded versions.

然后它将使用预先下载的版本。

回答by Maciej Gol

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

该问题已在聊天中解决(评论中的链接)。

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

问题在于使用sudo yum install pylint,因为它在全局环境中安装了 pylint。解决方案是使用以下命令:

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

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

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

请注意,-i对于提问者来说,常规索引似乎被破坏了。

回答by starlocke

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)

我也遇到了这个问题。我的解决方案只是简单地编辑 pylint 程序的shebang,就像这样......(不过,你的 pylint 路径可能与我的不同)

$ sudo vim /usr/bin/pylint

Replacing:

更换:

#!/usr/bin/python

With:

和:

#!/usr/bin/env python

回答by ThorSummoner

You can get there by calling the target python interpreter:

您可以通过调用目标 python 解释器到达那里:

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

# or in an already active env
python -m pylint ...

回答by Jér?me

Noufal Ibrahim's answerworks if you execute pylint manually.

如果您手动执行 pylint,Noufal Ibrahim 的回答有效。

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

如果从编辑器/IDE 执行 pylint,则需要正确配置插件。

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

它可能会变得棘手。这可能被认为是每个 IDE/插件的错误,但事实就是如此。

Modifying /usr/bin/pylintto write #!/usr/bin/env pythonas suggested in another answerfixes this for every use of pylint (manual use, or any editor integration).

修改/usr/bin/pylint#!/usr/bin/env python按照另一个答案中的建议编写可以针对每次使用 pylint(手动使用或任何编辑器集成)修复此问题

However, at least in Debian, using #!/usr/bin/pythonis a design choice, not a bug. See herefor the rationale.

但是,至少在 Debian 中,使用#!/usr/bin/python是一种设计选择,而不是错误。看这里的理由。

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

为了避免修改该系统文件,可以创建一个/usr/bin/pylintin副本/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".

这不会被 pylint 更新打破,但仍然侵犯了 Debian 的“强烈首选”。

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

此方法需要 root 权限。非特权用户可以创建别名

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


I always develop in virtualenv and I setup a postmkvirtualenv hook to install pylint and flake8 automatically when creating a virtualenv, so I don't use the versions ditributed by debian anymore.

我总是在 virtualenv 中开发,并且在创建 virtualenv 时我设置了一个 postmkvirtualenv 钩子来自动安装 pylint 和 flake8,所以我不再使用 debian 分发的版本。

回答by Omar Trejo

I know it's been a while since this question was answered, but I just thought I should leave this post here in case someone else runs into the same problem.

我知道这个问题已经有一段时间没有回答了,但我只是想我应该把这篇文章留在这里,以防其他人遇到同样的问题。

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

如果出于某种原因需要保留pylint在全局空间而不是虚拟环境中,则可以使用此处的建议:PyLint + VirtualEnv

It basically says to configure your pylintusing the init-hookand encoding version of a Python program that will use the global pylintand load the rest of the environment.

它基本上是说要pylint使用init-hookPython 程序的编码版本来配置您,该程序将使用全局pylint并加载环境的其余部分。

回答by mrArias

I'm using the Syntastic + 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 virtualenvcommand that, among some other things, installs pylintafter all the requirements.

我正在使用 Syntastic + Pylint 组合,并且由于我有许多不同的 virtualenv 可以在任何给定时间处理,因此我在virtualenv命令上创建了一个包装器,除其他外,在满足所有要求后安装pylint.

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

这样,每当我激活 virtualenv 时,我都会得到它自己的 pylint 版本。

Hope this helps, and thanks for the tip on deleting the global one from @briford-wylie

希望这会有所帮助,并感谢您提供从@briford-wylie 中删除全局一个的提示

回答by UncleSaam

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)

就在今天遇到同样的问题。继续ThorSummoner的回答,在诸如 Pipenv 之类的虚拟环境中使用 Pylint 和 pylint-django 时,请确保使用目标 python 解释器调用 pylint ( python -m pylint)

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:

一个很好的方法,它可以在本地和你的 CI 上工作,是在你的 Pipfile 的脚本部分写下 lint 命令:

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

Then calling pylint is as easy as :

然后调用 pylint 就像这样简单:

pipenv run lint