I'm running PyLint from inside Wing IDE on Windows. I have a sub-directory (package) in my project and inside the package I import a module from the top level, ie.
我在Windows上运行PyLint。我在我的项目中有一个子目录(包),在包里面我从顶层导入一个模块。
__init__.py
myapp.py
one.py
subdir\
__init__.py
two.py
Inside two.py
I have import one
and this works fine at runtime, because the top-level directory (from which myapp.py
is run) is in the Python path. However, when I run PyLint on two.py it gives me an error:
在两个。py我已经导入了一个,在运行时运行良好,因为它是*目录(myapp就是从这个目录中获取的)。py是运行的)在Python路径中。然而,当我运行PyLint 2。它给了我一个错误:
F0401: Unable to import 'one'
How do I fix this?
我怎么修复这个?
13 个解决方案
#1
83
There are two options I'm aware of.
我知道有两个选择。
One, change the PYTHONPATH
environment variable to include the directory above your module.
第一,将PYTHONPATH环境变量更改为包含模块上面的目录。
Alternatively, edit ~/.pylintrc
to include the directory above your module, like this:
另外,编辑~ /。pylintrc将包含模块上面的目录,如下所示:
[MASTER]
init-hook='import sys; sys.path.append("/path/to/root")'
(Or in other version of pylint, the init-hook requires you to change [General] to [MASTER])
(或者在其他版本的pylint中,init-hook要求您将[General]改为[MASTER])
Both of these options ought to work.
这两种选择都应该行得通。
Hope that helps.
希望有帮助。
#2
14
1) sys.path is a list.
1)系统。路径是一个列表。
2) The problem is sometimes the sys.path is not your virtualenv.path and you want to use pylint in your virtualenv
问题有时是系统的问题。路径不是你的艺术。路径和您想要在您的虚拟环境中使用pylint
3) So like said, use init-hook (pay attention in ' and " the parse of pylint is strict)
3)如上所述,使用init-hook(注意in '和“pylint的解析是严格的)
[Master]
init-hook='sys.path = ["/path/myapps/bin/", "/path/to/myapps/lib/python3.3/site-packages/", ... many paths here])'
or
或
[Master]
init-hook='sys.path = list(); sys.path.append("/path/to/foo")'
.. and
. .和
pylint --rcfile /path/to/pylintrc /path/to/module.py
#3
13
Do you have an empty __init__.py
file in both directories to let python know that the dirs are modules?
你有空房吗?让python知道两个目录中的py文件是模块吗?
The basic outline when you are not running from within the folder (ie maybe from pylint's, though I haven't used that) is:
当你不在文件夹内运行时(比如可能来自pylint,虽然我还没用过)的基本轮廓是:
topdir\
__init__.py
functions_etc.py
subdir\
__init__.py
other_functions.py
This is how the python interpreter is aware of the module without reference to the current directory, so if pylint is running from its own absolute path it will be able to access functions_etc.py
as topdir.functions_etc
or topdir.subdir.other_functions
, provided topdir
is on the PYTHONPATH
.
这就是python解释器在不引用当前目录的情况下了解模块的方式,因此,如果pylint从它自己的绝对路径运行,它将能够访问functions_etc。py topdir。functions_etc或topdir.subdir。other_functions,提供topdir位于PYTHONPATH上。
UPDATE: If the problem is not the __init__.py
file, maybe just try copying or moving your module to c:\Python26\Lib\site-packages
-- that is a common place to put additional packages, and will definitely be on your pythonpath. If you know how to do Windows symbolic links or the equivalent (I don't!), you could do that instead. There are many more options here: http://docs.python.org/install/index.html, including the option of appending sys.path with the user-level directory of your development code, but in practice I usually just symbolically link my local development dir to site-packages - copying it over has the same effect.
更新:如果不是问题所在。py文件,可能只是尝试复制或将您的模块移动到c:\Python26\Lib\site-package——这是放置附加包的常见地方,并且肯定会在您的pythonpath上。如果您知道如何做Windows符号链接或类似的链接(我不知道!),您可以这样做。这里有更多的选项:http://docs.python.org/install/index.html,包括appending sys的选项。使用您的开发代码的用户级目录的路径,但是在实践中,我通常只是象征性地将我的本地开发目录链接到站点包——复制它具有相同的效果。
#4
8
The solution to alter path in init-hook
is good, but I dislike the fact that I had to add absolute path there, as result I can not share this pylintrc file among the developers of the project. This solution using relative path to pylintrc file works better for me:
在init-hook中修改路径的解决方案是好的,但是我不喜欢必须在那里添加绝对路径,因此我不能在项目的开发人员*享这个pylintrc文件。使用相对路径到pylintrc文件的解决方案对我更有效:
[MASTER]
init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"
Note that pylint.config.PYLINTRC
also exists and has the same value as find_pylintrc()
.
注意,pylint.config。PYLINTRC也存在,其值与find_pylintrc()相同。
#5
5
I don't know how it works with WingIDE, but for using PyLint with Geany, I set my external command to:
我不知道如何使用WingIDE,但是对于使用PyLint和Geany,我将外部命令设置为:
PYTHONPATH=${PYTHONPATH}:$(dirname %d) pylint --output-format=parseable --reports=n "%f"
where %f is the filename, and %d is the path. Might be useful for someone :)
其中%f是文件名,%d是路径。可能对某些人有用:)
#6
3
I had to update the system PYTHONPATH
variable to add my App Engine path. In my case I just had to edit my ~/.bashrc
file and add the following line:
我必须更新系统PYTHONPATH变量来添加我的App引擎路径。在我的情况下,我只需要编辑我的~/。bashrc文件并添加如下行:
export PYTHONPATH=$PYTHONPATH:/path/to/google_appengine_folder
出口到PYTHONPATH = $ PYTHONPATH:/道路/ / google_appengine_folder
In fact, I tried setting the init-hook
first but this did not resolve the issue consistently across my code base (not sure why). Once I added it to the system path (probably a good idea in general) my issues went away.
事实上,我尝试先设置init-hook,但是这并没有在我的代码库中一致地解决问题(不知道为什么)。一旦我将它添加到系统路径(通常可能是个好主意),我的问题就消失了。
#7
2
One workaround that I only just discovered is to actually just run PyLint for the entire package, rather than a single file. Somehow, it manages to find imported module then.
我刚刚发现的一个解决方案是为整个包而不是单个文件运行PyLint。不知何故,它成功地找到了导入的模块。
#8
2
Try
试一试
if __name__ == '__main__':
from [whatever the name of your package is] import one
else:
import one
Note that in Python 3, the syntax for the part in the else
clause would be
注意,在Python 3中,else子句中部分的语法是
from .. import one
On second thought, this probably won't fix your specific problem. I misunderstood the question and thought that two.py was being run as the main module, but that is not the case. And considering the differences in the way Python 2.6 (without importing absolute_import
from __future__
) and Python 3.x handle imports, you wouldn't need to do this for Python 2.6 anyway, I don't think.
仔细想想,这可能不会解决你的具体问题。我误解了这个问题,认为是两个。py作为主模块运行,但事实并非如此。并且考虑到Python 2.6(不从__future__导入absolute_import)和Python 3的差异。x处理导入,对于Python 2.6你不需要这么做,我不认为。
Still, if you do eventually switch to Python 3 and plan on using a module as both a package module and as a standalone script inside the package, it may be a good idea to keep something like
不过,如果您最终切换到Python 3,并计划将模块作为包模块和包中的独立脚本使用,那么最好保留类似的内容
if __name__ == '__main__':
from [whatever the name of your package is] import one # assuming the package is in the current working directory or a subdirectory of PYTHONPATH
else:
from .. import one
in mind.
在心里的。
EDIT: And now for a possible solution to your actual problem. Either run PyLint from the directory containing your one
module (via the command line, perhaps), or put the following code somewhere when running PyLint:
编辑:现在为您的实际问题找到一个可能的解决方案。从包含您的一个模块的目录(可能通过命令行)运行PyLint,或者在运行PyLint时将以下代码放在某处:
import os
olddir = os.getcwd()
os.chdir([path_of_directory_containing_module_one])
import one
os.chdir(olddir)
Basically, as an alternative to fiddling with PYTHONPATH, just make sure the current working directory is the directory containing one.py
when you do the import.
基本上,作为修改PYTHONPATH的替代方法,只需确保当前工作目录是包含一个目录的目录。当你做导入时。
(Looking at Brian's answer, you could probably assign the previous code to init_hook
, but if you're going to do that then you could simply do the appending to sys.path
that he does, which is slightly more elegant than my solution.)
(查看Brian的答案,您可能会将前面的代码分配给init_hook,但是如果您要这样做,那么您可以简单地将追加到sys。他所做的路径,比我的解决方案略优雅一些。
#9
1
Maybe by manually appending the dir inside the PYTHONPATH?
可能是通过手动在PYTHONPATH中添加dir ?
sys.path.append(dirname)
#10
1
I had the same problem and since i could not find a answer I hope this can help anyone with a similar problem.
我也有同样的问题,既然我找不到答案,我希望这能帮助任何有类似问题的人。
I use flymake with epylint. Basically what i did was add a dired-mode-hook that check if the dired directory is a python package directory. If it is I add it to the PYTHONPATH. In my case I consider a directory to be a python package if it contains a file named "setup.py".
我用的是flymake和epylint。基本上,我所做的就是添加一个dired-mode-hook,它检查dired目录是否是python包目录。如果是我将它添加到PYTHONPATH中。在我的例子中,如果一个目录包含一个名为“setup.py”的文件,那么它就是一个python包。
;;;;;;;;;;;;;;;;;
;; PYTHON PATH ;;
;;;;;;;;;;;;;;;;;
(defun python-expand-path ()
"Append a directory to the PYTHONPATH."
(interactive
(let ((string (read-directory-name
"Python package directory: "
nil
'my-history)))
(setenv "PYTHONPATH" (concat (expand-file-name string)
(getenv ":PYTHONPATH"))))))
(defun pythonpath-dired-mode-hook ()
(let ((setup_py (concat default-directory "setup.py"))
(directory (expand-file-name default-directory)))
;; (if (file-exists-p setup_py)
(if (is-python-package-directory directory)
(let ((pythonpath (concat (getenv "PYTHONPATH") ":"
(expand-file-name directory))))
(setenv "PYTHONPATH" pythonpath)
(message (concat "PYTHONPATH=" (getenv "PYTHONPATH")))))))
(defun is-python-package-directory (directory)
(let ((setup_py (concat directory "setup.py")))
(file-exists-p setup_py)))
(add-hook 'dired-mode-hook 'pythonpath-dired-mode-hook)
Hope this helps.
希望这个有帮助。
#11
1
In case anybody is looking for a way to run pylint as an external tool in PyCharm and have it work with their virtual environments (why I came to this question), here's how I solved it:
如果有人正在寻找一种方法,将pylint作为PyCharm的外部工具来运行,并让它与他们的虚拟环境一起工作(我为什么要问这个问题),我是这样解决的:
- In PyCharm > Preferences > Tools > External Tools, Add or Edit an item for pylint.
- 在PyCharm >偏好>工具>外部工具中,为pylint添加或编辑一个条目。
- In the Tool Settings of the Edit Tool dialog, set Program to use pylint from the python interpreter directory:
$PyInterpreterDirectory$/pylint
- 在编辑工具对话框的工具设置中,将程序设置为使用python解释器目录中的pylint: $PyInterpreterDirectory$/pylint
- Set your other parameters in the Parameters field, like:
--rcfile=$ProjectFileDir$/pylintrc -r n $FileDir$
- 在parameters字段中设置其他参数,例如:——rcfile=$ProjectFileDir$/pylintrc -r n $FileDir$
- Set your working directory to
$FileDir$
- 将工作目录设置为$FileDir$。
Now using pylint as an external tool will run pylint on whatever directory you have selected using a common config file and use whatever interpreter is configured for your project (which presumably is your virtualenv interpreter).
现在使用pylint作为外部工具将在您使用公共配置文件选择的任何目录上运行pylint,并使用为项目配置的任何解释器(可能是您的virtualenv解释器)。
#12
1
The key is to add your project directory to sys.path
without considering about the env variable.
关键是将项目目录添加到sys。不考虑env变量的路径。
For someone who use VSCode, here's a one-line solution for you if there's a base directory of your project:
对于使用VSCode的人,如果您的项目有一个基本目录,这里有一个单行解决方案:
[MASTER]
init-hook='base_dir="my_spider"; import sys,os,re; _re=re.search(r".+\/" + base_dir, os.getcwd()); project_dir = _re.group() if _re else os.path.join(os.getcwd(), base_dir); sys.path.append(project_dir)'
Let me explain it a little bit:
我来解释一下
-
re.search(r".+\/" + base_dir, os.getcwd()).group()
: find base directory according to the editing filere.search(r”。+\ wd /" + base_dir, os. getcc().group():根据编辑文件查找基目录
-
os.path.join(os.getcwd(), base_dir)
: addcwd
tosys.path
to meet command line environment连接(os.getcwd(), base_dir):将cwd添加到sys。满足命令行环境的路径
FYI, here's my .pylintrc:
通知你,这是我.pylintrc:
https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f
https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f
#13
1
I had this same issue and fixed it by installing pylint in my virtualenv and then adding a .pylintrc file to my project directory with the following in the file:
我遇到了同样的问题,通过在我的virtualenv中安装pylint,然后在我的项目目录中添加了一个.pylintrc文件,文件中包含以下内容:
[Master]
init-hook='sys.path = list(); sys.path.append("./Lib/site-packages/")'
#1
83
There are two options I'm aware of.
我知道有两个选择。
One, change the PYTHONPATH
environment variable to include the directory above your module.
第一,将PYTHONPATH环境变量更改为包含模块上面的目录。
Alternatively, edit ~/.pylintrc
to include the directory above your module, like this:
另外,编辑~ /。pylintrc将包含模块上面的目录,如下所示:
[MASTER]
init-hook='import sys; sys.path.append("/path/to/root")'
(Or in other version of pylint, the init-hook requires you to change [General] to [MASTER])
(或者在其他版本的pylint中,init-hook要求您将[General]改为[MASTER])
Both of these options ought to work.
这两种选择都应该行得通。
Hope that helps.
希望有帮助。
#2
14
1) sys.path is a list.
1)系统。路径是一个列表。
2) The problem is sometimes the sys.path is not your virtualenv.path and you want to use pylint in your virtualenv
问题有时是系统的问题。路径不是你的艺术。路径和您想要在您的虚拟环境中使用pylint
3) So like said, use init-hook (pay attention in ' and " the parse of pylint is strict)
3)如上所述,使用init-hook(注意in '和“pylint的解析是严格的)
[Master]
init-hook='sys.path = ["/path/myapps/bin/", "/path/to/myapps/lib/python3.3/site-packages/", ... many paths here])'
or
或
[Master]
init-hook='sys.path = list(); sys.path.append("/path/to/foo")'
.. and
. .和
pylint --rcfile /path/to/pylintrc /path/to/module.py
#3
13
Do you have an empty __init__.py
file in both directories to let python know that the dirs are modules?
你有空房吗?让python知道两个目录中的py文件是模块吗?
The basic outline when you are not running from within the folder (ie maybe from pylint's, though I haven't used that) is:
当你不在文件夹内运行时(比如可能来自pylint,虽然我还没用过)的基本轮廓是:
topdir\
__init__.py
functions_etc.py
subdir\
__init__.py
other_functions.py
This is how the python interpreter is aware of the module without reference to the current directory, so if pylint is running from its own absolute path it will be able to access functions_etc.py
as topdir.functions_etc
or topdir.subdir.other_functions
, provided topdir
is on the PYTHONPATH
.
这就是python解释器在不引用当前目录的情况下了解模块的方式,因此,如果pylint从它自己的绝对路径运行,它将能够访问functions_etc。py topdir。functions_etc或topdir.subdir。other_functions,提供topdir位于PYTHONPATH上。
UPDATE: If the problem is not the __init__.py
file, maybe just try copying or moving your module to c:\Python26\Lib\site-packages
-- that is a common place to put additional packages, and will definitely be on your pythonpath. If you know how to do Windows symbolic links or the equivalent (I don't!), you could do that instead. There are many more options here: http://docs.python.org/install/index.html, including the option of appending sys.path with the user-level directory of your development code, but in practice I usually just symbolically link my local development dir to site-packages - copying it over has the same effect.
更新:如果不是问题所在。py文件,可能只是尝试复制或将您的模块移动到c:\Python26\Lib\site-package——这是放置附加包的常见地方,并且肯定会在您的pythonpath上。如果您知道如何做Windows符号链接或类似的链接(我不知道!),您可以这样做。这里有更多的选项:http://docs.python.org/install/index.html,包括appending sys的选项。使用您的开发代码的用户级目录的路径,但是在实践中,我通常只是象征性地将我的本地开发目录链接到站点包——复制它具有相同的效果。
#4
8
The solution to alter path in init-hook
is good, but I dislike the fact that I had to add absolute path there, as result I can not share this pylintrc file among the developers of the project. This solution using relative path to pylintrc file works better for me:
在init-hook中修改路径的解决方案是好的,但是我不喜欢必须在那里添加绝对路径,因此我不能在项目的开发人员*享这个pylintrc文件。使用相对路径到pylintrc文件的解决方案对我更有效:
[MASTER]
init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"
Note that pylint.config.PYLINTRC
also exists and has the same value as find_pylintrc()
.
注意,pylint.config。PYLINTRC也存在,其值与find_pylintrc()相同。
#5
5
I don't know how it works with WingIDE, but for using PyLint with Geany, I set my external command to:
我不知道如何使用WingIDE,但是对于使用PyLint和Geany,我将外部命令设置为:
PYTHONPATH=${PYTHONPATH}:$(dirname %d) pylint --output-format=parseable --reports=n "%f"
where %f is the filename, and %d is the path. Might be useful for someone :)
其中%f是文件名,%d是路径。可能对某些人有用:)
#6
3
I had to update the system PYTHONPATH
variable to add my App Engine path. In my case I just had to edit my ~/.bashrc
file and add the following line:
我必须更新系统PYTHONPATH变量来添加我的App引擎路径。在我的情况下,我只需要编辑我的~/。bashrc文件并添加如下行:
export PYTHONPATH=$PYTHONPATH:/path/to/google_appengine_folder
出口到PYTHONPATH = $ PYTHONPATH:/道路/ / google_appengine_folder
In fact, I tried setting the init-hook
first but this did not resolve the issue consistently across my code base (not sure why). Once I added it to the system path (probably a good idea in general) my issues went away.
事实上,我尝试先设置init-hook,但是这并没有在我的代码库中一致地解决问题(不知道为什么)。一旦我将它添加到系统路径(通常可能是个好主意),我的问题就消失了。
#7
2
One workaround that I only just discovered is to actually just run PyLint for the entire package, rather than a single file. Somehow, it manages to find imported module then.
我刚刚发现的一个解决方案是为整个包而不是单个文件运行PyLint。不知何故,它成功地找到了导入的模块。
#8
2
Try
试一试
if __name__ == '__main__':
from [whatever the name of your package is] import one
else:
import one
Note that in Python 3, the syntax for the part in the else
clause would be
注意,在Python 3中,else子句中部分的语法是
from .. import one
On second thought, this probably won't fix your specific problem. I misunderstood the question and thought that two.py was being run as the main module, but that is not the case. And considering the differences in the way Python 2.6 (without importing absolute_import
from __future__
) and Python 3.x handle imports, you wouldn't need to do this for Python 2.6 anyway, I don't think.
仔细想想,这可能不会解决你的具体问题。我误解了这个问题,认为是两个。py作为主模块运行,但事实并非如此。并且考虑到Python 2.6(不从__future__导入absolute_import)和Python 3的差异。x处理导入,对于Python 2.6你不需要这么做,我不认为。
Still, if you do eventually switch to Python 3 and plan on using a module as both a package module and as a standalone script inside the package, it may be a good idea to keep something like
不过,如果您最终切换到Python 3,并计划将模块作为包模块和包中的独立脚本使用,那么最好保留类似的内容
if __name__ == '__main__':
from [whatever the name of your package is] import one # assuming the package is in the current working directory or a subdirectory of PYTHONPATH
else:
from .. import one
in mind.
在心里的。
EDIT: And now for a possible solution to your actual problem. Either run PyLint from the directory containing your one
module (via the command line, perhaps), or put the following code somewhere when running PyLint:
编辑:现在为您的实际问题找到一个可能的解决方案。从包含您的一个模块的目录(可能通过命令行)运行PyLint,或者在运行PyLint时将以下代码放在某处:
import os
olddir = os.getcwd()
os.chdir([path_of_directory_containing_module_one])
import one
os.chdir(olddir)
Basically, as an alternative to fiddling with PYTHONPATH, just make sure the current working directory is the directory containing one.py
when you do the import.
基本上,作为修改PYTHONPATH的替代方法,只需确保当前工作目录是包含一个目录的目录。当你做导入时。
(Looking at Brian's answer, you could probably assign the previous code to init_hook
, but if you're going to do that then you could simply do the appending to sys.path
that he does, which is slightly more elegant than my solution.)
(查看Brian的答案,您可能会将前面的代码分配给init_hook,但是如果您要这样做,那么您可以简单地将追加到sys。他所做的路径,比我的解决方案略优雅一些。
#9
1
Maybe by manually appending the dir inside the PYTHONPATH?
可能是通过手动在PYTHONPATH中添加dir ?
sys.path.append(dirname)
#10
1
I had the same problem and since i could not find a answer I hope this can help anyone with a similar problem.
我也有同样的问题,既然我找不到答案,我希望这能帮助任何有类似问题的人。
I use flymake with epylint. Basically what i did was add a dired-mode-hook that check if the dired directory is a python package directory. If it is I add it to the PYTHONPATH. In my case I consider a directory to be a python package if it contains a file named "setup.py".
我用的是flymake和epylint。基本上,我所做的就是添加一个dired-mode-hook,它检查dired目录是否是python包目录。如果是我将它添加到PYTHONPATH中。在我的例子中,如果一个目录包含一个名为“setup.py”的文件,那么它就是一个python包。
;;;;;;;;;;;;;;;;;
;; PYTHON PATH ;;
;;;;;;;;;;;;;;;;;
(defun python-expand-path ()
"Append a directory to the PYTHONPATH."
(interactive
(let ((string (read-directory-name
"Python package directory: "
nil
'my-history)))
(setenv "PYTHONPATH" (concat (expand-file-name string)
(getenv ":PYTHONPATH"))))))
(defun pythonpath-dired-mode-hook ()
(let ((setup_py (concat default-directory "setup.py"))
(directory (expand-file-name default-directory)))
;; (if (file-exists-p setup_py)
(if (is-python-package-directory directory)
(let ((pythonpath (concat (getenv "PYTHONPATH") ":"
(expand-file-name directory))))
(setenv "PYTHONPATH" pythonpath)
(message (concat "PYTHONPATH=" (getenv "PYTHONPATH")))))))
(defun is-python-package-directory (directory)
(let ((setup_py (concat directory "setup.py")))
(file-exists-p setup_py)))
(add-hook 'dired-mode-hook 'pythonpath-dired-mode-hook)
Hope this helps.
希望这个有帮助。
#11
1
In case anybody is looking for a way to run pylint as an external tool in PyCharm and have it work with their virtual environments (why I came to this question), here's how I solved it:
如果有人正在寻找一种方法,将pylint作为PyCharm的外部工具来运行,并让它与他们的虚拟环境一起工作(我为什么要问这个问题),我是这样解决的:
- In PyCharm > Preferences > Tools > External Tools, Add or Edit an item for pylint.
- 在PyCharm >偏好>工具>外部工具中,为pylint添加或编辑一个条目。
- In the Tool Settings of the Edit Tool dialog, set Program to use pylint from the python interpreter directory:
$PyInterpreterDirectory$/pylint
- 在编辑工具对话框的工具设置中,将程序设置为使用python解释器目录中的pylint: $PyInterpreterDirectory$/pylint
- Set your other parameters in the Parameters field, like:
--rcfile=$ProjectFileDir$/pylintrc -r n $FileDir$
- 在parameters字段中设置其他参数,例如:——rcfile=$ProjectFileDir$/pylintrc -r n $FileDir$
- Set your working directory to
$FileDir$
- 将工作目录设置为$FileDir$。
Now using pylint as an external tool will run pylint on whatever directory you have selected using a common config file and use whatever interpreter is configured for your project (which presumably is your virtualenv interpreter).
现在使用pylint作为外部工具将在您使用公共配置文件选择的任何目录上运行pylint,并使用为项目配置的任何解释器(可能是您的virtualenv解释器)。
#12
1
The key is to add your project directory to sys.path
without considering about the env variable.
关键是将项目目录添加到sys。不考虑env变量的路径。
For someone who use VSCode, here's a one-line solution for you if there's a base directory of your project:
对于使用VSCode的人,如果您的项目有一个基本目录,这里有一个单行解决方案:
[MASTER]
init-hook='base_dir="my_spider"; import sys,os,re; _re=re.search(r".+\/" + base_dir, os.getcwd()); project_dir = _re.group() if _re else os.path.join(os.getcwd(), base_dir); sys.path.append(project_dir)'
Let me explain it a little bit:
我来解释一下
-
re.search(r".+\/" + base_dir, os.getcwd()).group()
: find base directory according to the editing filere.search(r”。+\ wd /" + base_dir, os. getcc().group():根据编辑文件查找基目录
-
os.path.join(os.getcwd(), base_dir)
: addcwd
tosys.path
to meet command line environment连接(os.getcwd(), base_dir):将cwd添加到sys。满足命令行环境的路径
FYI, here's my .pylintrc:
通知你,这是我.pylintrc:
https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f
https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f
#13
1
I had this same issue and fixed it by installing pylint in my virtualenv and then adding a .pylintrc file to my project directory with the following in the file:
我遇到了同样的问题,通过在我的virtualenv中安装pylint,然后在我的项目目录中添加了一个.pylintrc文件,文件中包含以下内容:
[Master]
init-hook='sys.path = list(); sys.path.append("./Lib/site-packages/")'