草庐IT

absolute_path

全部标签

python - 确保 py.test 在 sys.path 中包含应用程序目录

我有一个项目目录结构如下(我认为这很标准):my_projectsetup.pymypkg__init__.pyfoo.pytestsfunctionaltest_f1.pyunittest_u1.py我正在使用py.test作为我的测试框架,我希望能够在my_project目录中运行py.testtests我的测试。这确实有效,直到我尝试在测试中使用(例如)importmypkg导入我的应用程序代码。那时,我收到错误“没有名为mypkg的模块”。经过一番调查,似乎py.test使用sys.path中的测试文件目录运行测试,但不是运行py.test的目录。为了解决这个问题,我在test

python - os.getcwd() 与 os.path.abspath(os.path.dirname(__file__))

我正在使用os模块在我的Django项目settings.py文件中具有相对路径。变量SITE_ROOT设置为settings.py文件的当前工作目录,然后用于引用同样位于同一目录中的所有static/media目录。这是我的问题:printos.getcwd()printos.path.abspath(os.path.dirname(__file__))在settings.py中,上述语句都有相同的输出。但是只有当我使用SITE_ROOT=os.path.abspath(os.path.dirname(__file__))时我的模板才会加载Django在这里寻找模板:TEMPLATE

python - 用 os.path.join() 构造绝对路径

我想在python中构建一个绝对路径,同时对路径分隔符之类的东西保持相当的清醒。edit0:例如在我的文件系统/etc/init.d(或C:\etc\init.d在w32上),我只想从元素etc和init.d构造它(在w32上,我可能还需要一个磁盘ID,像C:)为了不必担心路径分隔符,os.join.path()显然是首选工具。但似乎这只会创建relative路径:print("MYPATH:%s"%(os.path.join('etc','init.d'),)MYPATH:etc/init.d添加一个虚拟的第一个元素(例如'')没有任何帮助:print("MYPATH:%s"%(os

python - Jupyter 和 Python 中的 sys.path 不同 - 如何在 Jupyter 中导入自己的模块?

在Jupyter中我自己的小模块没有加载,但在python/bpython中一切都很好。打字时importsysprint(sys.path)我的模块的路径不会在Jupyter中显示,但在python/bpython中它仍然存在。我正在使用:.bashrc中的PYTHONPATH以包含我的模块,虚拟环境中的Jupyter和bpython。最相似的问题是这个Cannotimportmodulesinjupyternotebook;wrongsys.path如何配置Jupyter以自动加载我的模块? 最佳答案 这是我在jupyterno

python - 我应该使用 np.absolute 还是 np.abs?

Numpy提供了np.absolute和别名np.abs通过定义from.numericimportabsoluteasabs这似乎明显违反了thezenofpython:Thereshouldbeone--andpreferablyonlyone--obviouswaytodoit.所以我猜这是有充分理由的。我个人在几乎所有代码中都使用np.abs并查看例如np.abs的搜索结果数对比np.absolute在StackOverflow上,似乎绝大多数人都这样做(2130对244次点击)。有什么理由我应该在我的代码中优先使用np.absolute而不是np.abs,或者我应该简单地选择

python - 在 Windows 上使用 os.path.join 混合斜线

我倾向于只对路径('/')使用正斜杠,python也很喜欢它在Windows上。在os.path.join的描述中,如果你想跨平台,这是正确的方法。但是当我使用它时,我得到了混合斜线:importosa='c:/'b='myFirstDirectory/'c='mySecondDirectory'd='myThirdDirectory'e='myExecutable.exe'printos.path.join(a,b,c,d,e)#Result:c:/myFirstDirectory/mySecondDirectory\myThirdDirectory\myExecutable.exe

python - 我应该使用哪一个SEP或OS.PATH.SEP?

他们是一样的,但是我应该使用哪一个?http://docs.python.org/library/os.html:os.sepThecharacterusedbytheoperatingsystemtoseparatepathnamecomponents.Thisis'/'forPOSIXand'\'forWindows.Notethatknowingthisisnotsufficienttobeabletoparseorconcatenatepathnames—useos.path.split()andos.path.join()—butitisoccasionallyuseful.

python - PEP8 - 使用 sys.path 导入不在文件顶部

问题PEP8有一条关于将导入放在文件顶部的规则:Importsarealwaysputatthetopofthefile,justafteranymodulecommentsanddocstrings,andbeforemoduleglobalsandconstants.但是,在某些情况下,我可能想做一些类似的事情:importsyssys.path.insert("..",0)importmy_module在这种情况下,pep8命令行实用程序会标记我的代码:E402modulelevelimportnotattopoffile通过sys.path修改实现PEP8合规性的最佳方法是什么

python - __path__ 有什么用?

在今天之前,我从未注意到在我的一些包上定义的__path__属性。根据文档:Packagessupportonemorespecialattribute,__path__.Thisisinitializedtobealistcontainingthenameofthedirectoryholdingthepackage’s__init__.pybeforethecodeinthatfileisexecuted.Thisvariablecanbemodified;doingsoaffectsfuturesearchesformodulesandsubpackagescontainedin

python - os.path.exists 与 os.path.isdir 之间的优缺点

我正在检查目录是否存在,但我注意到我使用的是os.path.exists而不是os.path.isdir。两者都工作得很好,但我很好奇使用isdir而不是exists有什么优势。 最佳答案 os.path.exists如果有一个具有该名称的常规文件,也将返回True。os.path.isdir仅当该路径存在并且是目录或指向目录的符号链接(symboliclink)时才会返回True。 关于python-os.path.exists与os.path.isdir之间的优缺点,我们在Stack