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

Ruff doesn't work with virtual environment #351

Closed
Vinci08 opened this issue Dec 1, 2023 · 19 comments · Fixed by #395
Closed

Ruff doesn't work with virtual environment #351

Vinci08 opened this issue Dec 1, 2023 · 19 comments · Fixed by #395
Labels
question Asking for support or clarification

Comments

@Vinci08
Copy link

Vinci08 commented Dec 1, 2023

Hello, I am having trouble get ruff to lint files inside a python virtual environment. The issue here is I kept getting warnings like this
Skipping standard library file: c:\Users\Leo\Desktop\someproject\api\_views\reports.py

here is my settings.json for ruff

	"ruff.interpreter": [
		"${workspaceFolder}\\scripts\\python.exe"
	],
	"python.defaultInterpreterPath": "${workspaceFolder}\\scripts\\python.exe",
	"[python]": {
		"editor.formatOnSave": true,
		"editor.codeActionsOnSave": {
			"source.fixAll": true,
			"source.organizeImports": true
		},
		"editor.defaultFormatter": "ms-python.black-formatter"
	},

Please give me some guidance on how to fix this issue. Thank you

@charliermarsh
Copy link
Member

That's strange! What do you see as the output of:

import site
print(site.getsitepackages())
print(site.getusersitepackages())

@Vinci08
Copy link
Author

Vinci08 commented Dec 1, 2023

@charliermarsh hi Charlie,

This is the input from print(site.getsitepackages())

['C:\\Users\\Leo\\Desktop\\someproject', 'C:\\Users\\Leo\\Desktop\\someproject\\lib\\site-packages']

This is the input from print(site.getusersitepackages())

C:\Users\Leo\AppData\Roaming\Python\Python38\site-packages

Thank you

@charliermarsh charliermarsh added the question Asking for support or clarification label Jan 16, 2024
@CrystalWindSnake
Copy link

You should not place any project code in the virtual environment directory.

https://docs.python.org/3/library/venv.html#module-venv

@Vinci08
Copy link
Author

Vinci08 commented Jan 16, 2024

@CrystalWindSnake hi Crystal, I used virtualenv, so the virtual environment directory is inside the project code, not the the other way around. At least that's how I interpret venv should be - different environments for different projects. Thank you.

@hansalemaos
Copy link

hansalemaos commented Jan 25, 2024

@CrystalWindSnake Sometimes it is unavoidable. I have just installed this extension, and have had the same problem (with Anaconda). This hack resolves the problem, but please add it as a check-box in the configuration settings.

# file on my hdd: C:\Users\xxx\.vscode\extensions\charliermarsh.ruff-2024.2.0-win32-x64\bundled\libs\ruff_lsp\server.py
# Hack 1:
# Comment out:

if document.is_stdlib_file():
   log_warning(f"Skipping standard library file: {document.path}")
   return None

# updated code should look like:

async def _run_format_on_document(document: Document) -> ExecutableResult | None:
    """Runs the Ruff `format` subcommand on the given document source."""
    # if document.is_stdlib_file():
    #    log_warning(f"Skipping standard library file: {document.path}")
    #    return None
	
async def _run_check_on_document(
    document: Document,
    *,
    extra_args: Sequence[str] = [],
    only: Sequence[str] | None = None,
) -> ExecutableResult | None:
    """Runs the Ruff `check` subcommand  on the given document source."""
    # if document.is_stdlib_file():
    #    log_warning(f"Skipping standard library file: {document.path}")
    #    return None

# Hack 2 (not tested):

def is_stdlib_file(self) -> bool:
	"""Return True if the document belongs to standard library."""
	return False # add this line
	return utils.is_stdlib_file(self.path) # ignored now (could be deleted)

@charliermarsh
Copy link
Member

@karthiknadig -- Have you seen this with any of the other extensions?

@Vinci08
Copy link
Author

Vinci08 commented Jan 25, 2024

@karthiknadig -- Have you seen this with any of the other extensions?

@charliermarsh I can confirm that ms-pylint has the same issue with virtual environment. This was the reason I looked into ruff.

@hansalemaos
Copy link

I'm moving from PyCharm to VsCode, and this was the first linter that worked in my Anaconda envs (with the hack though). By the way: It seems like doing a great job. Thank you for the great work!

@karthiknadig
Copy link

How is the project structured. This is what we expect:

project_dir
|- virtualEnv
|    |- lib
|        |- site-packages
|    |- bin
|        |- python
|- your_script.py
|- your_module
|    |- module_files.py

can you share your project structure?

@charliermarsh
Copy link
Member

By the way: It seems like doing a great job. Thank you for the great work!

Thank you so much :)

Right now I'm leaning towards adding a setting to allow folks to opt-out of this behavior.

@jdegenstein
Copy link

@hansalemaos I tried your workaround (hack 1), and I am getting some crashes.

ERROR Future exception was never retrieved
future: <Future finished exception=BrokenPipeError(32, 'The pipe has been ended', None, 109, None)>
Traceback (most recent call last):
  File "C:\Users\xxx\.conda\envs\ocp_vscode\Lib\asyncio\subprocess.py", line 152, in _feed_stdin
    await self.stdin.drain()
...

Not sure what I did wrong. Tried restarting VSCode/ruff a few times and the same result.

@hansalemaos
Copy link

@karthiknadig I guess that you are asking @Vinci08, right? I am using anaconda.
@charliermarsh You are welcome! :)
An opt-out would be great, because I frequently change things in lib/site-packages (modules that I wrote or sometimes monkey patches for other modules). And a linter would help there too!
Besides that, I often put py files directly in the env folder when I compile code with Nuitka or PyInstaller. That makes things much easier.

@hansalemaos
Copy link

hansalemaos commented Jan 25, 2024

@hansalemaos I tried your workaround (hack 1), and I am getting some crashes.

ERROR Future exception was never retrieved
future: <Future finished exception=BrokenPipeError(32, 'The pipe has been ended', None, 109, None)>
Traceback (most recent call last):
  File "C:\Users\xxx\.conda\envs\ocp_vscode\Lib\asyncio\subprocess.py", line 152, in _feed_stdin
    await self.stdin.drain()
...

Not sure what I did wrong. Tried restarting VSCode/ruff a few times and the same result.

I am using 1+2 combined now, and it is working. But it seems like, there will be a patch soon according to: @charliermarsh

This is the first and only code so far that I have used to test it

import pandas as pd
import numpy as np  # working here
from PrettyColorPrinter import add_printer
def bsbsbs(x=4, y=22):
    return x**x**y **zz # working here
kj;j # working here
add_printer(1)
df = pd.read_csv("C:\Users\xxx\Downloads\titanic.txt")  # working here

Some things that I should add:

  1. ruff is installed in my Anaconda base env with the "--user" flag
  2. The interpreter for the linter is explicitly defined as: C:\ProgramData\anaconda3\python.exe
  3. the path to ruff is explicitly defined as: C:\ProgramData\anaconda3\Scripts\ruff.exe
  4. C:\ProgramData\anaconda3 / C:\ProgramData\anaconda3\Scripts are in my PATH
  5. I am using Python 3.11
  6. I activate the env (NOT THE BASE!) before I open VsCode
  7. I open code.exe from the Windows terminal with the folder of the env as argument

@Vinci08
Copy link
Author

Vinci08 commented Jan 25, 2024

@karthiknadig I usually create new project folder (let's call it project_dir), then run virtualenv . at project folder level to create the virtual environment. This is my usual structure:

project_dir
|- my_module
| |- module_files.py
|- Scripts
|- Lib
|- my_script.py
|- requirements.txt
|- .gitignore

I put Lib and Script folder .gitignore, so the only thing makes it to source control is requirements.txt.

@karthiknadig
Copy link

That structure is not recommended. Please follow the one I posted. Soon venv (the python package) on creation will add a .gitignore (*) that will ignore the entire directory for source control. This is intentional as the directory is supposed to be destructible.

You can also use Python: Create Environment to create venv or conda envs. It will create them with the structure that we recommend.

@hansalemaos
Copy link

That structure is not recommended. Please follow the one I posted. Soon venv (the python package) on creation will add a .gitignore (*) that will ignore the entire directory for source control. This is intentional as the directory is supposed to be destructible.

You can also use Python: Create Environment to create venv or conda envs. It will create them with the structure that we recommend.

Drinking beer is also not recommended, but a lot of people drink. :) Don't you think there is a way to add a little checkbox in the settings to disable this behaviour. I would help those outlaws out there :)

@charliermarsh
Copy link
Member

I am somewhat tempted to remove this check altogether and put the onus on Ruff and the user's Ruff configuration to filter out these kinds of things.

@hansalemaos
Copy link

Awesome!!!

charliermarsh added a commit to astral-sh/ruff-lsp that referenced this issue Jan 25, 2024
charliermarsh added a commit that referenced this issue Jan 25, 2024
## Summary

Users can now specify `"ruff.ignoreStandardLIbrary": false` to turn off
the standard library detection.

Closes #351.
@charliermarsh
Copy link
Member

Setting (to opt-out) will go out in the next release (tomorrow, I gotta sleep and would prefer not to release and leave in case there are issues).

azurelotus0926 added a commit to azurelotus0926/ruff-lsp that referenced this issue Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants