Where is the actual file for a module? [duplicate] - python

This question already has answers here:
How do I find the location of Python module sources?
(20 answers)
Closed 5 years ago.
I apparently have multiple versions of a module installed and I am trying to figure out where the files are because only one of them is from package management and I want to delete the other.
Is there a simple way to ask Python where it found a module after importing it?

if the module isn't built-in, you can do this:
import your_module
print(your_module.__file__)
test:
>>> import os
>>> print(os.__file__)
L:\Python34\lib\os.py
if module is built-in, you get an error:
>>> import sys
>>> print(sys.__file__)
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
>>>
(check if module has the __file__ attribute using hasattr is also an option to avoid errors; if hasattr(module_name, '__file__'):)
also: by directly printing the module:
>>> print(os)
<module 'os' from 'L:\\Python34\\lib\\os.py'>

You can use __file__ after importing the module:
>>> import os
>>> os.__file__
'/usr/lib/python2.7/os.pyc'

Related

Can't import from a module imported as [duplicate]

This question already has answers here:
Why can't I import from a module alias?
(4 answers)
Closed 2 years ago.
Let's suppose I import the pathlib module with an alias :
import pathlib as plib
Then plib is now pointing to the pathlib module :
>>> plib
<module 'pathlib' from '/usr/lib/python3.8/pathlib.py'>
Now can someone tell me why importing something from this aliased module doesn't work?
>>> from plib import Path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'plib'
The loading process of The module is not based on the alias, it searches the python installation or project directory by the given name after import statement.

Why can't I import the re module?

I wanted to import the re module to do some web scraping.
I wrote down the 'import re' function and got this message:
Traceback (most recent call last):
File "/Users/willardsun/PycharmProjects/untitled/re.py", line 1, in <module><br>
import re<br>
File "/Users/willardsun/PycharmProjects/untitled/re.py", line 2, in <module><br>
re.compile<br>
AttributeError: partially initialized module 're' has no attribute 'compile' (most likely due to a circular import)
What does this exactly mean? I checked the binary skeleton and there was no re module. If the problem is due to this, then how do I install the module back? Thanks.
I think you are trying import re module in a .py file named 're.py'.
In this way, a name clash occurs.So why not change the name of the .py file into my_re.py?

Python Can't Find Importlib

I am trying to import the importlib module but I get this message:
>>> importlib
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'importlib' is not defined
I have manually located the importlib file within the Python folder, so I know I have it. I am running Python version 3.4.1. I also tried to pip install importlib but it wasn't found either.
What's going on?
You need to use an import statement to load it in:
>>> import importlib
>>> importlib
<module 'importlib' from 'C:\\Python33\\lib\\importlib\\__init__.py'>

How can you find where python imported a particular module from?

How can you find where python imported a particular module from?
Each module object has a __file__ attribute:
import module
print module.__file__
Some modules are part of the Python executable; these will not have the attribute set.
Demo:
>>> import urllib2
>>> urllib2.__file__
'/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.pyc'
>>> import sys
>>> sys.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
You could also run Python in verbose mode, with the -v command line switch or the PYTHONVERBOSE environment variable; Python then prints out every imported file as it takes place.

Module appears in module list but can't be imported [duplicate]

This question already has answers here:
ImportError: No module named _ssl
(9 answers)
Closed 9 years ago.
I'm getting an error because the ssl module isn't available
If I run help('modules') from the python interpreter it is listed there
When I try to import it from the interpreter, I get
>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/ssl.py", line 60, in <module>
import _ssl # if we can't import it, let the error propagate
Ensure that you have openssl package installed.

Categories

Resources