unused import statement ... is wrong?

已回答
Sometimes PyCharm 3.0.1 marks an import statement (like 'import os') as unused, but if I comment it out, the program doesn't run, saying 'NameError: name 'os' is not defined'.

What might be happening?
0
Please, specify the case of using import.
Also you can suppress inspection: use "Alt+Enter" and go to Optimize import -> suppress for statement (take a look at web-help http://www.jetbrains.com/pycharm/webhelp/suppressing-inspections.html)
0
Ok, I've narrowed it down; it's something to do with a namespace collision, but it really shouldn't have this problem anyway, I think.

It's very common to do "from pylab import *" – I know that's Bad, but the thing is, this has never caused issues until PyCharm 3.0.1 for me. (And http://wiki.scipy.org/PyLab claims that this is the CORRECT way to import pylab.)

Here's a minimal program that shows the problem:


    import os
    from pylab import *
    os.makedirs('foo')


For that code, pycharm claims that "import os" is unused, but if you comment it out, os is not defined.
0

Having a similar issue, saying it's unused... Not sure what's going on. 

 

import org.joda.time.LocalTime;

public class HelloWorld {
public static void main(String[] args) {
LocalTime currentTime = new LocalTime();
}
}
0

same here 

import numpy as xyz
np.random.seed (1)


NameError: name 'np' is not defined

0

Hi Thx2190,

You are creating an alias for numpy and call it xyz, but then you are trying to address it via np

The problem is not related to PyCharm.

You should either use import numpy as np instead of the first line of code or xyz.random.seed (1) instead of the second line of code.

0

Same issue here:

Unused import statemente 'import logging'

Unused import statemente 'import re'

 

import logging
import re



class Analysis(object):
"""
Performs analysis
"""
def __init__(self):
"""
Constructor

:return: None
"""
warnings.simplefilter(action='ignore', category=FutureWarning)
self._analysis_handler_logger = logging.getLogger('handler_analysis')

...

text = re.split('|'.join(outer_text_1), outer_text_2)
0

@Pablosolar R

What PyCharm version do you use?

Try File | Invalidate Caches / Restart... | Invalidate and Restart

1

Sergey Karpov

I am using 2020.1, build #PC-201.6668.115. 

I tried the invalidate option but same behaviour:

Unused import statemente 'import logging'

Unused import statemente 'import re'

Thank you!

0

Could you share a screenshot showing that in your code and the location of the file in your project?

0

Sergey Karpov sure, there you have an example. There is something curious... this behaviour only seems to happen in this class.

0

Any chance you could share the whole code snippet with the subclasses of that class?

I think it's something code-specific and probably related to the constructor overriding, but not I'm not 100% sure.

0

The above issue (Class uses of an import not being recognized as imports) is happening with me, too:

Initial import:

 

Initial Class definition (truncated):

 

A method within the class using the problematic import:

 

Note, this is a PyQt5 application class, and is very long - like, 800 lines long. It's for a corporate client too, so I can't share the full code. But I'll gladly answer questions to the best of my ability if you need more context.

0

Hello Matthew

If it is possible, please provide me with a simplified code example, so I will be able to reproduce it on my site and investigate it. 

 

Thank you in advance. 

0

That's a known issue with Django 3.1 https://youtrack.jetbrains.com/issue/PY-43897
Adding import os to settings.py should do the trick

>Can I write in russian here?))))) Or only english?

Better use English so other users will understand.

0

Oh, excuse me, I've created the separate theme about this..... I thought, it would be better...

It's here

 

Thank you for quick reply!

0

No problem. In fact, it's better to create a new ticket/post when the problem is different.

1

Hope this helps. for anyone who might search for the same question, it means you have not used anything from the imported module, so there is no point importing it.

-3

Minyichen1997 

Made an account just to up-vote your answer. Is the most logical and simple answer. Don't know why everyone else is just mumbling non-sense to this easy-to-understand error. 

Thanks! 

-9

Floreact

See other screenshots. The modules are in fact used in the code but highlighted as unused by PyCharm.

1

The claims in other comments that this is the expected behavior are not correct.

I have been seeing this error throughout our production code, and have determined at least one set of circumstances that leads to this behavior. I have created the following minimal example that exhibits a false 'unused import' warning, when in fact the import is required.

This example has a module-level import of the logger package, as well as an inline import within main. I appears that the inline import leads to the false 'unused import' warning on the wrong import. Note, this example executes with no errors.

When the module-level import for logger is removed, the warning goes away. However, the execution fails because the module is not imported, as shown here.

Although the work-around in this scenario is simple, it is more challenging in production code when inline imports exists somewhere in the file. More importantly, running 'Optimize imports' will automatically remove the module-level import, resulting in code that will no longer execute.

The correct behavior would be for the warning to appear on the inline import (on line 18 of my example). And 'Optimize imports' should remove this inline import, not the module-level import.

My version of PyCharm is PyCharm 2022.1.4 (Professional Edition) Build #PY-221.6008.17, built on July 20, 2022.

Python version is Python 3.10.6

2

The issues with false unused imports in __name__ = "__main__" is covered as part of Issue PY-55905

0

请先登录再写评论。