Python: "re" module is included in "Tkinter" module? - python

I'm using Python 3.6.4 with Miniconda on MacOS. I'm curious about that I can use methods that belongs to re module after I import tkinter. For example, if I want to use a re method without importing it:
>>> re.compile('abc')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 're' is not defined
But if I use a re methods after importing tkinter, it works properly, like:
>>> from tkinter import *
>>> re.compile('abc')
re.compile('abc')
So I can use re.compile() even if I haven't done import re. Why does it happen?

From the tkinter Source in line 39:
import re
So when you import the Module tkinter you automaticly import re with it

Related

Downloading Modules on Python

Im looking to download modules on Python but I seem to be running into some problems. Whenever I import a new module, an error always shows up. Here is an example of the code that I am using to download a module:
import openpyxl
and this is the error that shows up when I try to run this code:
Traceback (most recent call last):
File "/Users/jonathankent/PycharmProjects/untitled/HelloWorld/automation.py", line 1, in <module>
import openpyxl
File "/Users/jonathankent/PycharmProjects/untitled/HelloWorld/venv/lib/python3.8/site-packages/openpyxl/__init__.py", line 4, in <module>
from openpyxl.compat.numbers import NUMPY, PANDAS
File "/Users/jonathankent/PycharmProjects/untitled/HelloWorld/venv/lib/python3.8/site-packages/openpyxl/compat/__init__.py", line 4, in <module>
from .strings import safe_string
File "/Users/jonathankent/PycharmProjects/untitled/HelloWorld/venv/lib/python3.8/site-packages/openpyxl/compat/strings.py", line 4, in <module>
from math import isnan, isinf
ImportError: cannot import name 'isnan' from 'math' (/Users/jonathankent/PycharmProjects/untitled/HelloWorld/math.py)
Process finished with exit code 1
If anyone knows what could be causing this please let me know.
ImportError: cannot import name 'isnan' from 'math' (/Users/jonathankent/PycharmProjects/untitled/HelloWorld/math.py)
It seems you have named your own module math. This shadows the built-in math module.
The error means that some other code wanted to import the built-in math module but found your module instead.
The simplest solution would be if you named your module differently.

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 module not callable when importing getopt

I am new to python and the getopt function. I am trying to import getopt, however I run into an error.
My code is literally just:
import getopt
and also tried
from getopt import * /// from getopt import getopt
Output below:
python asdfasdf.py
ARGV : []
Traceback (most recent call last):
File "asdfasdf.py", line 1, in <module>
import getopt
File "/home/STUDENTS/~~/csc328/TCP/pyExample/getopt.py", line 12, in <module>
'version=',
TypeError: 'module' object is not callable
My python is 2.6 something and this was implemented in 2.3 I believe.
edit: I'm using unix
you might be have getotp module in your code. When you refere python third party module, it try to import your module.
Pleas remove getopt.py in your example or rename it.

How to import a Python module into Jython?

I can use the module correctly by Python, but when using Jython, some error occurs...
Code:
from jieba import *
Errors:
Traceback (most recent call last):
File "/Users/Jack/Documents/workspace/FirstJython/hellojyphon.py", line 8, in <module>
from jieba import *
File "/Users/Jack/Documents/workspace/FirstJython/jieba/__init__.py", line 15, in <module>
from ._compat import *
ImportError: No module named _compat
Is there any differences between Python and Jython when import?
I solve it by myself.
There is something wrong when using relative directories in Jython.
so after I change ._compat to jieba._compat, the problem solved!
But I don't exactly know the reason...

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'>

Categories

Resources