草庐IT

Python __init__.py 与 sys.path.append/insert

我know那里are一个ton的how-to进口Pythonmodules不在路径中,但我还没有遇到过使用Python的__init.py__与sys.path.insert。哪种方法更好?是否有任何明显的缺点,比如性能?还有一个“Pythonic”吗?我能想到的一个场景是,我有一个用户下载并放在任何目录中的程序,所以我不知道绝对路径(除非我以编程方式获取它)。文件夹结构为workingdir__init__.pyfoo.pysrc/my_utils.py__init__.py我看不出使用__init__.py或更改sys.path有什么区别。您是否可以想到任何情况会有所作为?我的问题

python - 一起使用 re.MULTILINE 和 re.DOTALL python

基本上输入文件是这样的:>U51677Humannon-histonechromatinproteinHMG1(HMG1)gene,completecds.#somerecordsdon'thavethisline(seebelow)Length=2575(sometext)>U51677Humannon-histonechromatinproteinHMG1(HMG1)gene,completeLength=2575(sometext)(etc...)现在我写这个来提取以>开头的行和长度的数字importreregex=re.compile("^(>.*)\r\n.*Length\s

python - 如何在 Python 中使用正则表达式 re.sub() 一个可选的匹配组?

我的问题很简单。我有一个URL,有时它以特定字符结尾。如果它们存在,我想将它们添加到我的新URL。test1="url#123"test2="url"r=re.sub(r"url(#[0-9]+)?",r"new_url\1",test1)#Expectedresult:"new_url#123"#Actualresult:"new_url#123"r=re.sub(r"url(#[0-9]+)?",r"new_url\1",test2)#Expectedresult:"new_url"#Actualresult:"error:unmatchedgroup"当然,我不能只做re.sub

python - 获取 Path 对象的 "true"主干的干净方法?

预期的输入和输出:a->aa.txt->aarchive.tar.gz->archivedirectory/file->filed.x.y.z/f.a.b.c->flogs/date.log.txt->date#Mine!这是我觉得很脏的实现:>>>frompathlibimportPath>>>example_path=Path("August082015,01'37'30.log.txt")>>>example_path.stem"August082015,01'37'30.log">>>example_path.suffixes['.log','.txt']>>>suffixes

python - 使用 re.findall 查找前 x 个匹配项

我需要限制re.findall找到前3个匹配项然后停止。例如text='some1text2bla3regex4python5're.findall(r'\d',text)然后我得到:['1','2','3','4','5']我想要:['1','2','3'] 最佳答案 re.findall返回一个列表,所以最简单的解决方案就是使用slicing:>>>importre>>>text='some1text2bla3regex4python5'>>>re.findall(r'\d',text)[:3]#Getthefirst3item

python - 通过 importlib 以编程方式导入模块 - __path__ 未设置?

我正在尝试以编程方式导入子模块。我的文件树如下所示:oopsd/__init__.pyoopsd/oopsd.pyoopsd/driver/__init__.pyoopsd/driver/optiups.pyoptiups.py只是打印“HelloWorld”。oopsd.py看起来像这样:importimportlibimportlib.import_module('oopsd.driver.optiups')现在有了这个,我得到了这个异常(exception):Traceback(mostrecentcalllast):File"",line1521,in_find_and_loa

python - 将目录添加到 Python sys.path,以便每次使用 Python 时都包含它

目前,当尝试引用一些库代码时,我在我的python文件的顶部这样做:importsyssys.path.append('''C:\code\my-library''')frommy-libraryimportmy-library然后,只要session处于事件状态,my-library就会成为sys.path的一部分。如果我开始一个新文件,我必须记得再次包含sys.path.append。我觉得一定有更好的方法来做到这一点。如何使my-library可用于我的Windows机器上的每个python脚本,而不必每次都使用sys.path.append? 最佳答

python - 异常 : "dot" not found in path in python on mac

我想使用caffe.draw在mac上通过anacondapython绘制caffe网络。但是我得到了这样的错误:File"python/draw_net.py",line45,inmain()File"python/draw_net.py",line41,inmaincaffe.draw.draw_net_to_file(net,args.output_image_file,args.rankdir)File"/Users/xxh/caffe/distribute/python/caffe/draw.py",line222,indraw_net_to_filefid.write(dr

python - 在 Pyramid 中获取当前路线而不是 route_path

我有导航栏,比如:12我想添加一些代码来测试当前的route是什么,这样我就可以决定要突出显示哪个栏(class="current")。我知道Pyramid有一个方法current_route_path来获取当前URL的路径。但是,我认为最好使用routename而不是routepath。有人对此有想法吗? 最佳答案 你想要的是使用matched_route.ifrequest.matched_route.name=='my_route_name': 关于python-在Pyramid中

python - 为什么 PyCharm 有时不使用系统环境变量作为虚拟环境的 PATH?

使用PyCharm,我配置了一个虚拟环境。这导致与PATH的一些非常困惑的交互。当我查看文件具有的PATH环境变量时:print'#####PATH'printos.environ['PATH']我看到以下输出:#####PATH/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/.virtualenvs/venv/bin这让我感到困惑,因为我已经通过以下对话框明确告诉PyCharm使用系统环境变量:单击“显示”时,我看到PATH的定义不同:我不明白的是:使用charm从shell运行PyCharm导致成功设置PATH:#####PATH/Use