The file name is the same as the package name to be quoted
For example, if you want to quote requests, but you name your own file also requests.py, then execute the following code
import requests
requests.get('http://www.baidu.com')
Will report the following error
AttributeError: module 'requests' has no attribute 'get'
The solution is to change the name of your python file, as long as it is not the same as the package name. If you really don’t want to change the file name, you can use the following method
import sys
_cpath_ = sys.path[0]
print(sys.path)
print(_cpath_)
sys.path.remove(_cpath_)
import requests
sys.path.insert(0, _cpath_)
requests.get('http://www.baidu.com')
The main principle is to exclude the current directory from the search directory of python running. After this processing, executing python requests.py on the command line can run normally, but debugging and running in pycharm fails.
Format misalignment problem
Below is a piece of normal code
def fun():
a=1
b=2
if a>b:
print("a")
else:
print("b")
fun()
1. If the else is not aligned
def fun():
a=1
b=2
if a>b:
print("a")
else:
print("b")
fun()
Will report
IndentationError: unindent does not match any outer indentation level
2. If else and if do not appear in pairs, such as directly writing an else or writing an extra else, or omission of the colon after if and else
def fun():
a=1
b=2
else:
print("b")
fun()
def fun():
a=1
b=2
if a>b:
print("a")
else:
print("b")
else:
print("b")
fun()
def fun():
a=1
b=2
if a>b:
print("a")
else
print("b")
fun()
Metropolis
SyntaxError: invalid syntax
3. If the statements below if and else are not indented
def fun():
a=1
b=2
if a>b:
print("a")
else:
print("b")
fun()
Will report
IndentationError: expected an indented block
Use Chinese quotation marks for strings
For example, use Chinese quotation marks below
print(“a”)
Will report
SyntaxError: invalid character in identifier
The correct way is to use English single or double quotes
print('b')
print("b")