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

module-import-not-at-top-of-file ignore after os.environ #10059

Closed
Pastells opened this issue Feb 20, 2024 · 4 comments · Fixed by #10066
Closed

module-import-not-at-top-of-file ignore after os.environ #10059

Pastells opened this issue Feb 20, 2024 · 4 comments · Fixed by #10066
Assignees
Labels
rule Implementing or modifying a lint rule

Comments

@Pastells
Copy link

Sometimes you need to set an environment variable before importing a package, for example:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "4" 
os.environ["WORLD_SIZE"] = "1"
import torch

See: https://discuss.pytorch.org/t/runtimeerror-device-0-device-num-gpus-internal-assert-failed/178118/6

My suggestion is to ignore E402 module-import-not-at-top-of-file if it comes after a os.environ statement, similar to #9047

@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Feb 20, 2024
@charliermarsh
Copy link
Member

I think I agree with this. We already ignore sys.path manipulations, and for me this is similar.

@charliermarsh
Copy link
Member

Added in #10066, though it'll be --preview-only until we release v0.3.0, per our versioning policy.

charliermarsh added a commit that referenced this issue Feb 20, 2024
…02`) (#10066)

## Summary

Allows, e.g.:

```python
import os

os.environ["WORLD_SIZE"] = "1"
os.putenv("CUDA_VISIBLE_DEVICES", "4")

import torch
```

For now, this is only allowed in preview.

Closes #10059
nkxxll pushed a commit to nkxxll/ruff that referenced this issue Mar 10, 2024
…02`) (astral-sh#10066)

## Summary

Allows, e.g.:

```python
import os

os.environ["WORLD_SIZE"] = "1"
os.putenv("CUDA_VISIBLE_DEVICES", "4")

import torch
```

For now, this is only allowed in preview.

Closes astral-sh#10059
@ion-elgreco
Copy link

@charliermarsh it might make sense to allow warnings.filterwarnings in between as well. I sometimes have to add that in between imports to silence the warnings

@Cjkjvfnby
Copy link
Contributor

I just use extend-per-file-ignores in my Ruff config, to suppress this rule for that specific file.

In my case it's custom logger configuration. I need to setup logger, before calling any getLogger. Since I use logger in other files, I have to do it before imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants