简体   繁体   English

Python 相对路径

[英]Python Relative path

I'm having some weird issues regarding relative path.我在相对路径方面遇到了一些奇怪的问题。 I have a backup framework that works well.我有一个运行良好的备份框架。 And after some update (merely adding py classes but not on that level) my relative path is not working.经过一些更新(仅添加 py 类但不在那个级别)我的相对路径不起作用。 Already checked my back up data still works.已经检查过我的备份数据仍然有效。 Comparing both framework and it shows it doesn't have any changes on codes.比较这两个框架,它表明它在代码上没有任何变化。 here is my code:这是我的代码:

The error:错误:

os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'chromedriver_v78.0.exe' executable needs to be in PATH. os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException:消息:'chromedriver_v78.0.exe' 可执行文件需要在 PATH 中。 Please see https://sites.google.com/a/chromium.org/chromedriver/home请参阅https://sites.google.com/a/chromium.org/chromedriver/home

def Open_chrome_browser(url):
    driver = webdriver.Chrome("..\\drivers\\chromedriver_v78.0.exe")

enter image description here在此处输入图片说明

The one having a problem is here:有问题的人在这里:

enter image description here在此处输入图片说明

No changes in code.代码没有变化。 The first image works.第一张图片有效。 Second wont.第二不会。

Is there a needed config?有需要的配置吗?

Edited:编辑:

Drivers are updated.驱动程序已更新。 No changes in path路径没有变化

enter image description here在此处输入图片说明

Dont know if this will help but the one in green circle is working when i put this code: sys.path.append(os.path.dirname( file ))不知道这是否有帮助,但是当我输入此代码时,绿色圆圈中的那个正在工作:sys.path.append(os.path.dirname( file ))

but the yellow is not working但黄色不起作用

You can get the current file path by using of the following codes.您可以使用以下代码获取当前文件路径。

import clr
import os
import sys

sys.path.append(os.path.dirname(__file__))

A relative path is always relative to the current working directory of the script that is executing.相对路径始终相对于正在执行的脚本的当前工作目录。 So, that's not (or at least not always) relative to the file the reference is in.因此,这与引用所在的文件无关(或至少并非总是如此)。

You can set the current working directory in the run configuration in PyCharm.您可以在 PyCharm 的运行配置中设置当前工作目录。 But for your script to work everywhere, you should ensure the current working directory is what you expect it to be for the script and check your paths are relative to that.但是为了让您的脚本在任何地方都可以工作,您应该确保当前的工作目录是您对脚本的期望,并检查您的路径是否与该目录相关。

Useful commands are os.getcwd() and os.chdir() .有用的命令是os.getcwd()os.chdir()

The default working directory for a script in PyCharm is the directory the script you start itself is in, outside of PyCharm it would be whatever directory you start Python with the script from, or the working directory set in a shortcut. PyCharm 中脚本的默认工作目录是您启动脚本本身所在的目录,在 PyCharm 之外,它将是您使用脚本启动 Python 的任何目录,或者是在快捷方式中设置的工作目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM