Author ArtOfCode Studio
Recipients ArtOfCode Studio, ezio.melotti, vstinner
Date 2020-02-11.19:35:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1581449751.58.0.336622448133.issue39612@roundup.psfhosted.org>
In-reply-to
Content
I want to find all imported modules in a python program. I am using the ``modulefinder`` standard module for my job. I am trying to follow [this example](https://docs.python.org/3.8/library/modulefinder.html#example-usage-of-modulefinder) on docs, but I get this error even if I use the same code as the documents:

````python
Traceback (most recent call last):
  File "C:\Users\Dinçel\Desktop\Deploy\zipperutils\find modules.py", line 4, in <module>
    finder.run_script('bacon.py')
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 165, in run_script
    self.load_module('__main__', fp, pathname, stuff)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 360, in load_module
    self.scan_code(co, m)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 433, in scan_code
    self._safe_import_hook(name, m, fromlist, level=0)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 378, in _safe_import_hook
    self.import_hook(name, caller, level=level)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 177, in import_hook
    q, tail = self.find_head_package(parent, name)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 233, in find_head_package
    q = self.import_module(head, qname, parent)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 326, in import_module
    m = self.load_module(fqname, fp, pathname, stuff)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 360, in load_module
    self.scan_code(co, m)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 433, in scan_code
    self._safe_import_hook(name, m, fromlist, level=0)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 378, in _safe_import_hook
    self.import_hook(name, caller, level=level)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 177, in import_hook
    q, tail = self.find_head_package(parent, name)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 233, in find_head_package
    q = self.import_module(head, qname, parent)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 326, in import_module
    m = self.load_module(fqname, fp, pathname, stuff)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 360, in load_module
    self.scan_code(co, m)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 433, in scan_code
    self._safe_import_hook(name, m, fromlist, level=0)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 378, in _safe_import_hook
    self.import_hook(name, caller, level=level)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 177, in import_hook
    q, tail = self.find_head_package(parent, name)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 233, in find_head_package
    q = self.import_module(head, qname, parent)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 326, in import_module
    m = self.load_module(fqname, fp, pathname, stuff)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 360, in load_module
    self.scan_code(co, m)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 466, in scan_code
    self.scan_code(c, m)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 433, in scan_code
    self._safe_import_hook(name, m, fromlist, level=0)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 378, in _safe_import_hook
    self.import_hook(name, caller, level=level)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 177, in import_hook
    q, tail = self.find_head_package(parent, name)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 233, in find_head_package
    q = self.import_module(head, qname, parent)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 326, in import_module
    m = self.load_module(fqname, fp, pathname, stuff)
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\modulefinder.py", line 343, in load_module
    co = compile(fp.read()+'\n', pathname, 'exec')
  File "C:\Users\Dinçel\AppData\Local\Programs\Python\Python38\lib\encodings\cp1254.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 308: character maps to <undefined>
````
My operating system is Windows 10.

I am using Python3.8.1

If there is a better way of finding imported modules let me know it.

Also is there any way of saying that to Python: "use unicode for directory names"?
History
Date User Action Args
2020-02-11 19:35:51ArtOfCode Studiosetrecipients: + ArtOfCode Studio, vstinner, ezio.melotti
2020-02-11 19:35:51ArtOfCode Studiosetmessageid: <1581449751.58.0.336622448133.issue39612@roundup.psfhosted.org>
2020-02-11 19:35:51ArtOfCode Studiolinkissue39612 messages
2020-02-11 19:35:51ArtOfCode Studiocreate