0

On Windows 10, running Python 3.10.4, programs are unable to find modules that are installed.

This program:

import statistics
print(statistics.mean([100, 90]))

Produces this result:

PS C:\VSC-files> python statistics.py
Traceback (most recent call last):
  File "C:\VSC-files\statistics.py", line 2, in <module>
    import statistics
  File "C:\VSC-files\statistics.py", line 4, in <module>
    print(statistics.mean([100, 90]))
AttributeError: partially initialized module 'statistics' has no attribute 'mean' (most likely due to a circular import)

Importing other modules produces similar results. Statistics module seems to be already installed, when I try to install it I get:

PS C:\VSC-files> python -m pip install statistics
Requirement already satisfied: statistics in c:\users\richard\appdata\local\programs\python\python310\lib\site-packages (1.0.3.5)
Requirement already satisfied: docutils>=0.3 in c:\users\richard\appdata\local\programs\python\python310\lib\site-packages (from statistics) (0.18.1)

Files are located at:

C:\Users\Richard\AppData\Local\Programs\Python\Python310\python.exe
C:\Users\Richard\AppData\Local\Programs\Python\Python310\Lib\site-packages\statistics
C:\VSC-files\statistics.py

Environmental Variable - Path:

C:\Users\Richard\AppData\Local\Programs\Python\Python310\Scripts\;C:\Users\Richard\AppData\Local\Programs\Python\Python310\;C:\Users\Richard\AppData\Local\Microsoft\WindowsApps;;C:\Users\Richard\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Richard\AppData\Local\Programs\Fiddler
martineau
  • 119,623
  • 25
  • 170
  • 301
  • The problem is your source file has the same name as the standard library module `statistics` that's being imported (so it's importing itself). Rename your script to something that does not conflict. – martineau Apr 08 '22 at 00:09
  • You are correct. However I was almost led astray. When I saved the file as stats.py (and also average.py) it still produced the same error. Then I realized, that I not only had to used a different file name, but had to remove my statistics.py file from the directory. Thank you. – Wizard4119 Apr 08 '22 at 01:55
  • Indeed, by default Python searches the current working directory for imported modules first — allowing one to easily override those in the standard library — so doing what you did it was still allowing the importation of your original script which imports itself. Avoiding that was why I suggest you *rename* your file. The major clue for me was the "`partially initialized module 'statistics' has no attribute 'mean'`" which often indicates this sort of thing is going on. – martineau Apr 08 '22 at 02:13

0 Answers0