草庐IT

functions_package

全部标签

python - 导入错误 : with error 'is not a package'

在python3中遇到ImportError问题。我的项目结构如下:cts_sap_polaris/|--etc||--clean_cts_sap_polaris.yaml||--clean_env_variables.tcl||--cts_sap_polaris_ha_combined.yaml||--cts_sap_polaris.yaml|`--TCL_TESTBED_CONFIGS|--__init__.py|--jobs||--__init__.py||--__pycache__||`--run_cts_sap_polaris.cpython-34.pyc|`--run_ct

python - 在 Python 中传递 "pointer to a virtual function"作为参数

在C++中比较以下代码:#include#includestructA{virtualvoidbar(void){std::coutobjs,void(A::*fun)()){for(autoo=objs.begin();o!=objs.end();++o){A*obj=(*o);(obj->*fun)();}}intmain(){std::vectorobjs={newA(),newB()};test(objs,&A::bar);}在Python中:classA:defbar(self):print("one")classB(A):defbar(self):print("two")d

python - 从 Cloud Function 写入 Google Cloud Storage(python)

我正在尝试从云函数中将文件上传到谷歌云存储。不过,我无法将云存储库导入到我的函数中。能否以这种方式在云函数中使用云存储?云函数fromgoogle.cloudimportstoragedefupload_blob(bucket_name,blob_text,destination_blob_name):"""Uploadsafiletothebucket."""storage_client=storage.Client()bucket=storage_client.get_bucket(bucket_name)blob=bucket.blob(destination_blob_name

Python 导入优先级 : packages or modules?

我不清楚如何正确命名这个问题。案例一假设我有以下目录结构。foo|+-bar/__init__.py|+-bar.py如果我有fromfooimportbar我如何知道正在导入哪个柱(bar.py或bar/__init__.py)?有什么简单的方法可以自动检测这种情况的发生吗?案例二foo|+-foo.py|+-other.py如果other.py有这行importfoo我如何知道正在导入哪个foo(foo或foo.foo)?同样,是否有任何简单的方法可以自动检测这种情况的发生? 最佳答案 TLDR;如果包在同一目录中,则包优先于同

python - 比较 : import statement vs __import__ function

作为问题的跟进Usingbuiltin__import__()innormalcases,我领导了一些测试,并得出了令人惊讶的结果。我在这里比较经典的import语句和调用__import__内置函数的执行时间。为此,我在交互模式下使用以下脚本:importtimeitdeftest(module):t1=timeit.timeit("import{}".format(module))t2=timeit.timeit("{0}=__import__('{0}')".format(module))print("importstatement:",t1)print("__import__f

python - 为什么 `function` 不是 Python 中的关键字?

str或type类>>>type("pear")>>>type(str)可在Python中访问:>>>str>>>type但是,类function和builtin_function_or_method不是。>>>deffoo():pass...>>>type(foo)>>>type(print)它们显示为内置类,但尝试访问它们会抛出名称错误:>>>functionTraceback(mostrecentcalllast):File"",line1,inNameError:name'function'isnotdefined>>>builtin_function_or_methodTra

Python 聊天 : delete variables to clean memory in functions?

我正在用python和twisted框架创建一个聊天守护进程。而且我想知道当多个用户连接时,我是否必须删除我的函数中创建的每个变量以从长远来看节省内存,或者这些变量是否会自动清除?这是我的代码的精简版本,用于说明我的观点:classChat(LineOnlyReceiver):LineOnlyReceiver.MAX_LENGTH=500deflineReceived(self,data):self.sendMessage(data)defsendMessage(self,data):try:message=data.split(None,1)[1]exceptIndexError:r

python - Pyinstaller 错误 ImportError : No module named 'requests. packages.chardet.sys

我似乎找不到这个问题的根本原因。我不知道是pyinstaller、pip问题、requests模块还是其他问题,因为无法最终排除。我用python编写了一个脚本,当我们必须部署一个新单元时,它可以为我们的企业网络正确配置一个新的硬件sonicwall。它在内存中配置一个正确的.exp文件,使用默认凭证登录到sonicwall设备,通过多部分数据表单导入文件,重新启动sonicwall,然后再次登录并正确更改共享key。出于安全原因,我不能在这里发布代码,但我可以用一个更简单的例子来解释这个问题。以前,代码使用urllib和urllib2来处理http请求,但是当我不得不重写脚本以包含c

python 3 : Change default values of existing function's parameters?

我正在创建一个程序,它最终会调用500次print函数,还有一些其他函数。这些函数中的每一个每次都会采用完全相同的参数,如下所示:print(a,end='-',sep='.')print(b,end='-',sep='.')print(c,end='-',sep='.')print(...,end='-',sep='.')有没有办法改变print函数参数的默认值?这样我就不必每次都输入end='-',sep='.'了吗? 最佳答案 您可以使用functools.partial()定义特殊版本的print()给它默认参数:fromf

python - 导入错误 : No module named pip when trying to install packages

使用PyCharm全新安装Ubuntu13.10,在设置python解释器时,我选择了“installsetuptools”,然后是“installpip”。现在,如果我尝试使用pip做任何事情,我会得到以下信息:ciaran@ciaran-desktop:~/pycharm/bin$pipTraceback(mostrecentcalllast):File"/usr/local/bin/pip",line9,inload_entry_point('pip==1.4.1','console_scripts','pip')()File"build/bdist.linux-x86_64/e