Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preferred way to find pylintrc in pylint 3 #8862

Closed
qi55wyqu opened this issue Jul 18, 2023 · 13 comments · Fixed by #9068 or #9093
Closed

Preferred way to find pylintrc in pylint 3 #8862

qi55wyqu opened this issue Jul 18, 2023 · 13 comments · Fixed by #9068 or #9093

Comments

@qi55wyqu
Copy link

Question

I've been using pylint.config.PYLINTRC in the init-hook of the pylintrc to set up the PYTHONPATH.

init-hook="
      import os, pathlib, pylint, sys
      testDir = pathlib.Path(pylint.config.PYLINTRC).resolve().parent.joinpath("test")
      sys.path.append(os.fspath(testDir))"

In pylint 3 this leads to an error

AttributeError: module 'pylint.config' has no attribute 'PYLINTRC'

This method was much more convenient and portable than setting the PYTHONPATH outside of pylint.
What is the preferred way to do this in pylint 3?

Documentation for future user

Documentation, release notes

Additional context

No response

@qi55wyqu qi55wyqu added Documentation 📗 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling Question labels Jul 18, 2023
@Pierre-Sassoulas
Copy link
Member

Why were you not using the --rcfile command line option ?

@qi55wyqu
Copy link
Author

I set the init-hook in pylintrc and called

pylint --rcfile pylintrc  /path/to/file.py

but got an AttributeError.

I'm using python 3.11.4 with pylint 3.0.0a6.

@Pierre-Sassoulas
Copy link
Member

Could you provide the stacktrace, please ?

@DanielNoord
Copy link
Collaborator

The constant was removed in 3318aa0#diff-24934165194ae0ca6e01c8e20fc7f7f24b54766260e02cac072c057a80cece49 following a deprecation period.

I don't really understand your use case, but can't you just do a os.getcwd() to get the directory name?

@Pierre-Sassoulas
Copy link
Member

I understood now, remove the init-hook and use the rcfile option.

@qi55wyqu
Copy link
Author

Stacktrace:

  File "/home/USER/.local/share/virtualenvs/tmp.Tmyq3RRWpX-tOmAVYQd/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
             ^^^^^^^^^^^^
  File "/home/USER/.local/share/virtualenvs/tmp.Tmyq3RRWpX-tOmAVYQd/lib/python3.11/site-packages/pylint/__init__.py", line 34, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "/home/USER/.local/share/virtualenvs/tmp.Tmyq3RRWpX-tOmAVYQd/lib/python3.11/site-packages/pylint/lint/run.py", line 165, in __init__
    args = _config_initialization(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/USER/.local/share/virtualenvs/tmp.Tmyq3RRWpX-tOmAVYQd/lib/python3.11/site-packages/pylint/config/config_initialization.py", line 50, in _config_initialization
    exec(utils._unquote(config_data["init-hook"]))  # pylint: disable=exec-used
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 3, in <module>
AttributeError: module 'pylint.config' has no attribute 'PYLINTRC'

If I use os.getcwd() it only works when I'm in the same directory as the pylintrc but not when I run it from a different directory.

If I remove the init-hook, I get import-errors because the PYTHONPATH is missing the directories that I added in the init-hook.
It only works if I export PYTHONPATH before calling pylint, which unfortunately is less convenient than setting everything up in the init-hook.

@Pierre-Sassoulas Pierre-Sassoulas removed the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jul 25, 2023
@Pierre-Sassoulas
Copy link
Member

If you want to change the pylintrc location then use the CLI's rcfile option, if you want to modify the sys.path in init-hook it's possible too, but do not use internal pylint API for that. You can modify the sys.path without accessing pylint.config.PYLINTRC.

@DanielNoord
Copy link
Collaborator

I think this is actually a common use case? You might be in a directory where one of the parent directories has your pylintrc and your tests directory.
Using the file and adding the directory to your path seems like something people might want. Perhaps we should expose the finding of the config file as public api? We did previously.

@Pierre-Sassoulas
Copy link
Member

Then we would need to make _find_config_in_home_or_environment public and document it. But I'm not sure what @qi55wyqu is trying to accomplish (I'm pretty sure it is an A / B problem). I don't see how a coupling with the pylintrc can be the easiest solution to whatever problem we're trying to solve here. Isn't the git root or the code root set ? Is the pylintrc moving so much that hard coding a directory in CLI is impossible ? Isn't it something that a proper virtulenv and editable pip install would solve ? init-hook is not something that should be used all the time without a very good reason imo.

@sauing
Copy link

sauing commented Feb 26, 2024

This issue is observed while running flake8-pylint from pre-commit as additional dependencies for flake8.
flake8-pylint version: 0.2.1
flake8 version: 6.0.0
pre-commit: 3.6.0
Stack Trace:
flake8...................................................................Failed

  • hook id: flake8
  • exit code: 1

Traceback (most recent call last):
File "C:\Users\factory\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\factory\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\Scripts\flake8.EXE_main
.py", line 7, in
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\main\cli.py", line 23, in main
app.run(argv)
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\main\application.py", line 198, in run
self._run(argv)
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\main\application.py", line 187, in _run
self.run_checks()
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\main\application.py", line 103, in run_checks
self.file_checker_manager.run()
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\checker.py", line 236, in run
self.run_serial()
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\checker.py", line 214, in run_serial
self.results = [
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\checker.py", line 219, in
).run_checks()
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\checker.py", line 524, in run_checks
self.run_ast_checks()
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8\checker.py", line 426, in run_ast_checks
for (line_number, offset, text, _) in runner:
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\flake8_pylint_plugin.py", line 92, in run
Run([self.filename], reporter=reporter, exit=False)
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\pylint\lint\run.py", line 162, in init
args = _config_initialization(
File "C:\Users\factory.cache\pre-commit\repoujy51n3r\py_env-python3.10\lib\site-packages\pylint\config\config_initialization.py", line 57, in _config_initialization
exec(utils._unquote(config_data["init-hook"])) # pylint: disable=exec-used
File "", line 2, in
AttributeError: module 'pylint.config' has no attribute 'PYLINTRC'

Yaml file:
image

@DanielNoord
Copy link
Collaborator

@sauing Please update your init-hook to no longer reference PYLINTRC.

@sauing
Copy link

sauing commented Feb 26, 2024

@sauing Please update your init-hook to no longer reference PYLINTRC.

Thanks it works :) It was strange that all of sudden it stopped working for pre-commit since i moved to conda environment

@jacobtylerwalls jacobtylerwalls removed their assignment Feb 26, 2024
@Pierre-Sassoulas
Copy link
Member

In this case it seems we need to fix the Doc on stackoverflow too: #9105 (comment)

duckduckgrayduck added a commit to MuckRock/documentcloud that referenced this issue Mar 28, 2024
duckduckgrayduck added a commit to MuckRock/documentcloud that referenced this issue Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants