Python Forum
NameError – function doesn't recognize imported modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError – function doesn't recognize imported modules
#4
1. as asked by @ndc85430 - why would you import like this in the first place?
2. you are right - it's a scope issue. that's one of the reasons why the recommended way for imports is at the top of the module

def spam():
    import os
    print(os.getcwd())

spam()
print(os.getcwd())
3. if you really, really, really need to do something like this - don't use eval or exec. use importlib modue
import importlib

def bulk_import(*modules):
    return (importlib.import_module(some_module) for some_module in modules)
    
os, datetime = bulk_import("os", 'datetime')
print(os.getcwd())
bowlofred likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: NameError – function doesn't recognize imported modules - by buran - Nov-30-2020, 08:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python cant recognize PIP siubikYT 2 779 Jul-19-2023, 06:30 PM
Last Post: Lahearle
  Getting NameError for a function that is defined JonWayn 2 1,129 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,926 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  Sharing imported modules with Sub Processes? Stubblemonster 2 1,524 May-02-2022, 06:42 AM
Last Post: Stubblemonster
  Visual Studio Code Intellisense for Imported Modules Not Working hockinsk 1 2,744 Apr-23-2022, 04:41 PM
Last Post: deanhystad
  Can a module tell where it is being imported from? stevendaprano 3 1,203 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,165 Feb-25-2022, 06:31 PM
Last Post: jao
  My program won't recognize the filename. braingoblins 1 1,140 Jan-07-2022, 06:18 PM
Last Post: deanhystad
  module detecting if imported vs not Skaperen 1 1,689 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  string function doesn't work in script ClockPillow 3 2,431 Jul-13-2021, 02:47 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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