栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python日常bug

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python日常bug

这里写目录标题
  • Python3.7 使用时报错
  • wandb
    • Login
  • dataloader
    • RuntimeError: freeze_support()
      • 全局变量:

Python3.7 使用时报错

urllib.error.URLError:

Solution1
将以下block copy至要跑的代码前。

import urllib.request
import ssl
def main():
	ssl._create_default_https_context = ssl._create_unverified_context
	response = urllib.request.urlopen('https://www.python.org')

	Your codes start here......

Solution2

conda install -c anaconda certifi
wandb Login

每个新项目都要login一次wandb,在https://wandb.ai/ 找到API 直接填入terminal,例如:

wandb login 123abc42623e2649730766b922bb71f40effbcc6
dataloader RuntimeError: freeze_support()

BrokenPipeError: [Errno 32] Broken pipe
File “xxxxpython3.7libmultiprocessingspawn.py”, in _check_not_importing_main is not going to be frozen to produce an executable.

RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom in the main module:

Solution:
将程序用if __name__ == '__main__':包裹起来,或者更全面:

if __name__ == '__main__':
                freeze_support()

(1)产生原因是Windows没有fork,只有Unix有,是一个可以帮助child process识别parent process 的
(2)对于python 里的multiprocessing, if __name__ == '__main__':不可少。

全局变量:

Bear in mind that if code run in a child process tries to access a global variable, then the value it sees (if any) may not be the same as the value in the parent process at the time that Process.start was called.

However, global variables which are just module level constants cause no problems.Safe importing of main module. Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).

For example, using the spawn or forkserver start method running the following module would fail with a RuntimeError:

from multiprocessing import Process
def foo():
    print('hello')
p = Process(target=foo)
p.start()

Instead one should protect the “entry point” of the program by using if __name__ == '__main__':as follows:

from multiprocessing import Process, freeze_support, set_start_method
def foo():
    print('hello')

if __name__ == '__main__':
    freeze_support()
    set_start_method('spawn')
    p = Process(target=foo)
    p.start()

The freeze_support() line can be omitted if the program will be run normally instead of frozen.This allows the newly spawned Python interpreter to safely import the module and then run the module’s foo() function.

Similar restrictions apply if a pool or manager is created in the main module.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号