I have a very large project with deeply nested hierarchies. To find the root directory I use a source root walker and append the root directory to sys.path. This way, I can easily import things in the project relative to the root directory. The problem is that pylint doesn't detect this, even though I have a method which appends the path imported in the files where the path is used. So all my imports are marked as F0401 <http://pylint-messages.wikidot.com/messages:f0401>: Unable to import. Is there any way to avoid this error message by getting pylint to understand sys.path? The __init__.py that is imported in files that need imports relative to the root directory: import os import sys def find_repo_rootdir(): def __check_parent(topdir): for _, _, files in os.walk(topdir): if '.THIS_IS_ROOT' in files: return topdir else: next_ = topdir.rsplit("/", 1) if len(next_) == 2: return __check_parent(next_[0]) else: return None return __check_parent(os.path.abspath(os.path.dirname(__file__))) repo_root = find_repo_rootdir() if repo_root is not None and repo_root not in sys.path: sys.path.append(repo_root)
On Thu, Mar 12, 2015 at 3:51 PM, Dani Hodovic <danihgit@gmail.com> wrote:
I have a very large project with deeply nested hierarchies. To find the root directory I use a source root walker and append the root directory to sys.path. This way, I can easily import things in the project relative to the root directory. The problem is that pylint doesn't detect this, even though I have a method which appends the path imported in the files where the path is used. So all my imports are marked as F0401 <http://pylint-messages.wikidot.com/messages:f0401>: Unable to import.
Is there any way to avoid this error message by getting pylint to understand sys.path?
The __init__.py that is imported in files that need imports relative to the root directory:
import os import sys
def find_repo_rootdir(): def __check_parent(topdir): for _, _, files in os.walk(topdir): if '.THIS_IS_ROOT' in files: return topdir else: next_ = topdir.rsplit("/", 1) if len(next_) == 2: return __check_parent(next_[0]) else: return None return __check_parent(os.path.abspath(os.path.dirname(__file__)))
repo_root = find_repo_rootdir() if repo_root is not None and repo_root not in sys.path: sys.path.append(repo_root)
_______________________________________________ code-quality mailing list code-quality@python.org https://mail.python.org/mailman/listinfo/code-quality
Hi Dani, Please subscribe to the list so the rest of your posts will not need to be moderated. Thanks, Ian
* Dani Hodovic <danihgit@gmail.com> [2015-03-12 21:51:50 +0100]:
I have a very large project with deeply nested hierarchies. To find the root directory I use a source root walker and append the root directory to sys.path. This way, I can easily import things in the project relative to the root directory.
I don't know how to teach pylint to understand this - but if I understood correctly, shouldn't python packages to that anyways? e.g. if you have a tree like this: myproject/ __init__.py utils/ __init__.py foo.py bar.py modules/ __init__.py eggs.py bacon.py spam.py And start your project correctly via `python -m myproject`, you should be able to do something like `from myproject.utils.foo import fish` in e.g. myproject/modules/eggs.py just fine. If that is what you're attempting, I recommend reading http://bit.ly/pypackages. If not and I misunderstood something, sorry! :) Florian -- http://www.the-compiler.org | me@the-compiler.org (Mail/XMPP) GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc I love long mails! | http://email.is-not-s.ms/
On 3/12/15 4:51 PM, Dani Hodovic wrote:
I have a very large project with deeply nested hierarchies. To find the root directory I use a source root walker and append the root directory to sys.path. This way, I can easily import things in the project relative to the root directory. The problem is that pylint doesn't detect this, even though I have a method which appends the path imported in the files where the path is used. So all my imports are marked as F0401 <http://pylint-messages.wikidot.com/messages:f0401>: Unable to import.
If you want to import everything relative to the root directory, why not set PYTHONPATH to point to the root directory, without sys.path manipulations? Then pylint will run the same way, and will find all of your imports. --Ned.
participants (4)
-
Dani Hodovic
-
Florian Bruhin
-
Ian Cordasco
-
Ned Batchelder