Python Forum
Pylance: "... is not a known attribute of module ..." ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pylance: "... is not a known attribute of module ..." ?
#1
Getting this "EXAMPLE" is not a known attribute of module "config" from Pylance (after activating Pylance)
But I don't understand why Pylance is complaining about this. Or what other ways (see below) I could use to fix this.

IDE: VSC

Related code in main py file:
from sys import path as sys_path
sys_path.insert(1, r"<path to py includes>") ## this path is not in the used main.py file-tree.
# ...
import config as CFG ## from <path to py includes> location.
# ...
CFG.DEBUG_ANSI = True ## no complains from pylance.
CFG.EXAMPLE = 123 ## pylance complaining about it.
Tried adding type annotation here. But pylance was not happy about those either. "Type annotation not supported for this statement".

Related code in config.py file:
Contain only a set of variables/settings (with some default(off) value) I generally use in other files/projects.
And nothing else. So no "def class" stuff either.
DEBUG_LEVEL = 0
# ... etc
DEBUG_ANSI = False
"CFG.EXAMPLE" is not included in the config.py file. I could ofc add it to the config.py file.
But the point here is that still would like to use this CFG import for global stuff in the main py file/project. Instead of using "EXAMPLE=123" at root level of the main py file.
+(O yea. Adding " # type: ignore" is not really an option either in my/this case.)

... Please educate me on this.
... Please assume I'm not well versed in code or Pyton terminology.
Reply
#2
Is "EXAMPLE" assigned a value in config.py? Dynamically adding attributes to objects is generally a bad thing. Often it is the result of a typo, such as:
CFG.debug_level = 1
Now your config module has a DEBUG_LEVEL and a debug_level attribute. How is pylance to know if this is the desired result or a mistake?

It looks like config is some sort of blackboard that you use to share objects between modules. The problem with this is config.EXAMPLE is not defined when the module is imported. EXAMPLE is only added to config after main.py assigns the value. I write another module that uses config.EXAMPLE.

junk.py
import config
print(config.EXAMPLE)
Importing junk may result in an attribute error depending on when the config.EXAMPLE is created and when it is used.

This runs without error:
[python]import config
config.EXAMPLE = 123
import junk
This results in an attribute error.
import config
import junk
config.EXAMPLE = 123
Error:
AttributeError: module 'config' has no attribute 'EXAMPLE'
The interesting thing is I cannot get pylance to generate the message you report. What are your pylance settings? What version are you using?
Reply
#3
pylance: v2025.6.2 (25 June 2025) Release. Type checking mode: Basic. I have not touched any other setting as far as I know.
Reply
#4
(Jul-06-2025, 11:45 AM)MvGulik Wrote: "CFG.EXAMPLE" is not included in the config.py file. I could ofc add it to the config.py file.
But the point here is that still would like to use this CFG import for global stuff in the main py file/project. Instead of using "EXAMPLE=123" at root level of the main py file.
Yes you could add EXAMPLE = None in config file and that will fix it.
Can just to do this if want to make many # type: ignore at top then ignore all.
# type: ignore
import config as CFG 

CFG.DEBUG_ANSI = True 
CFG.EXAMPLE = 123
CFG.TEST = 456
CFG.car = 'BMV'
Or just ignored it all together if you know is right.
Pylance (and Pyright) are designed to catch typos and other common mistakes before you run your code.
When you dynamically add attributes to a module at runtime, that pattern is perfectly legal in Python,
but static type checkers need a hint, or they’ll warn you that it’s probably a typo.
Reply
#5
(Jul-07-2025, 09:50 AM)snippsat Wrote: When you dynamically add attributes to a module at runtime, that pattern is perfectly legal in Python,
but static type checkers need a hint, or they’ll warn you that it’s probably a typo.
Yea. That part is sinking in more clearly with time.

# type: ignore
import config as CFG
That did not do anything (yet) I'm afraid (before or behind the line in question). At least not with my current Pylance settings. Will explore Pylance settings in more dept.

(Jul-07-2025, 09:50 AM)snippsat Wrote: Or just ignored it all together if you know is right.
In that case I think I go with tuning down my CFG dynamic added stuff and individually mark them to be ignored. Just to get rid of the red warning bars in the code preview (of which I have a lot - other particular case I'm still looking into).
Reply
#6
Might as well add this github pylance-issues talk about the subject. (after a bit more reading up on the subject)
"Type annotation not supported for this type of expression" when dynamically adding a property.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "openpyxl" is not accessed Pylance dee 6 94 Apr-22-2026, 03:09 PM
Last Post: dee
  module 'openpyxl.workbook' has no attribute 'active' lsaavedra21 5 3,918 Oct-30-2024, 06:26 PM
Last Post: lsaavedra21
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 4,451 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  Module 'time' has no attribute 'clock' Sophie 4 8,150 Jan-25-2022, 08:05 PM
Last Post: Sophie
  AttributeError: module 'string' has no attribute 'uppercase' Anldra12 10 16,138 Apr-23-2021, 05:30 PM
Last Post: ibreeden
  Python linter Pylance: What does "(variable): List | Unbound" mean? Drone4four 1 9,845 Oct-23-2020, 08:31 PM
Last Post: bowlofred
  module 'os' has no attribute 'tmpfile' whois1230 3 6,795 Jun-12-2020, 03:59 AM
Last Post: ndc85430
  AttributeError: module 'collections' has no attribute 'namedtuple' epgs1975 2 12,712 May-04-2020, 08:10 PM
Last Post: epgs1975
  AttributeError: module 'platform' has no attribute 'python_implementation' davidpluseipi 3 7,663 May-01-2020, 11:47 PM
Last Post: Larz60+
  AttributeError: module 'asyncio' has no attribute 'get_event_loop Susmitha 5 24,664 Sep-03-2019, 02:33 AM
Last Post: chengonghao

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020