ImportError: No module named 'custom function'
Phenomenon: Use the command line to run a file in the package, and an error similar to the following will appear
$ python tests/test_utils.py
Traceback (most recent call last):
File "tests/test_utils.py", line 7, in <module>
from run_api import app
ImportError: No module named 'run_api'
Solution: Add the program root directory to the variablePYTHONPATH
进入到程序根目录
export PYTHONPATH=.
windows 环境执行:set PYTHONPATH=.
# 然后执行相关命令
python tests/test_utils.py
Transcoding is abnormal in some cases in Windows environment
Phenomenon: CMD under Windows executes normally, but executes in the console provided by the third-party system and reports an error similar to the following
UnicodeEncodeError
'gbk' codec can't encode character '\xa0' in position 22: illegal multibyte sequence
at F:\Program Files\Python39\lib\site-packages\clikit\io\output_stream\stream_output_stream.py:24 in write
20│ """
21│ if self.is_closed():
22│ raise io.UnsupportedOperation("Cannot write to a closed input.")
23│
→ 24│ self._stream.write(string)
25│ self._stream.flush()
26│
27│ def flush(self): # type: () -> None
28│
Workaround: Configure the variable before executing the commandPYTHONIOENCODING=utf-8
进入到程序根目录
PYTHONIOENCODING=utf-8
windows 环境执行:set PYTHONIOENCODING=utf-8
# 然后执行相关命令
poetry help
Use the module operate svn to report an authentication failure errorsubprocess
Phenomenon: There is no problem in directly using the command line to operate svn, but when using python to operate the svn command, an error similar to the following is reported
svn: E215004: No more credentials or we tried too many times.
Authentication failed
Workaround: use the parameter--username --password --no-auth-cache
Use shutil.rmtree to delete directory report permission error under Windows
Phenomenon: The execution has an error similar to the followingshutil.rmtree(path)
WindowsError: [Error 5] Access is denied: 'build\\tcl\\tcl8.5\\msgs\\af.msg'
The general situation is because the file has a read-only attribute, the solution: add the onerror method
import os
import stat
import shutil
def remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)
shutil.rmtree(top, onerror=remove_readonly)
Batch remove read-only attribute of directories under Windows
def remove_windows_readonly(path: Union[str, list]):
if isinstance(path, str):
path = [path]
if isinstance(path, list):
for i in path:
subprocess.run(f"attrib -R {i}/* /S /D")
If the function decorated by the decorator has a return value, it must be returned on the decorator at the same time, otherwise the subsequent call to the function will not be able to obtain the return value