我有一个名为的脚本requests.py导入请求包.该脚本无法访问包中的属性,也无法导入它们.为什么这不起作用,我该如何解决?
以下代码提出了一个问题AttributeError.
import requests
res = requests.get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
以下代码提出了一个问题ImportError.
from requests import get
res = get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get' …Run Code Online (Sandbox Code Playgroud) 如何找到当前运行的Python脚本的完整路径?也就是说,我需要做些什么来实现这个目标:
$ pwd
/tmp
$ python baz.py
running from /tmp
file is baz.py
Run Code Online (Sandbox Code Playgroud) 如何获取正在运行的Python脚本的名称?
我试过os.__file__但是返回了os所在文件的名称.
如果您运行的是冷冻python脚本从一个目录(使用py2exe冻结)和驱动从那里脚本存在不同的,什么是确定执行脚本的路径的最佳方式?
我尝试过的解决方案很少
inspect.getfile(inspect.currentframe())
Run Code Online (Sandbox Code Playgroud)
问题:不返回完整路径.它只返回脚本名称.
os.path.abspath( __file__ )
Run Code Online (Sandbox Code Playgroud)
问题:在Windows上不起作用
os.path.dirname(sys.argv[0])
Run Code Online (Sandbox Code Playgroud)
问题:返回空字符串.
os.path.abspath(inspect.getsourcefile(way3))
Run Code Online (Sandbox Code Playgroud)
如果驱动器与pwd不同,则无法工作
os.path.dirname(os.path.realpath(sys.argv[0]))
Run Code Online (Sandbox Code Playgroud)
如果驱动器与pwd不同,则无法工作
这是一个最小的不工作的例子
D:\>path
PATH=c:\Python27\;c:\Users\abhibhat\Desktop\ToBeRemoved\spam\dist\;c:\gnuwin32\bin
D:\>cat c:\Users\abhibhat\Desktop\ToBeRemoved\spam\eggs.py
import os, inspect, sys
def way1():
return os.path.dirname(sys.argv[0])
def way2():
return inspect.getfile(inspect.currentframe())
def way3():
return os.path.dirname(os.path.realpath(sys.argv[0]))
def way4():
try:
return os.path.abspath( __file__ )
except NameError:
return "Not Found"
def way5():
return os.path.abspath(inspect.getsourcefile(way3))
if __name__ == '__main__':
print "Path to this script is",way1()
print "Path to this script is",way2()
print "Path to this script is",way3()
print "Path to this script is",way4()
print …Run Code Online (Sandbox Code Playgroud) 所以有很多非常相似的问题,但没有一个答案似乎满足我正在寻找的东西.
基本上我在命令行中使用绝对目录运行python脚本.
在这个文件本身,我想导入一个模块/文件,我目前使用绝对路径来做这个(sys.path.append(/....).
但是我想使用一个相对路径,相对于脚本本身.
我似乎能做的就是追加相对于我目前工作目录的路径.
我该怎么做呢?
我想在python中创建当前工作目录之外的文件.这是我的目录结构.
|--myproject
| |-- gui
| | |-- modules
| | | |-- energy
| | | | |-- configuration
| | | | | |-- working_file.py
| |-- service
| | |-- constants
| | | |-- global_variables.json
Run Code Online (Sandbox Code Playgroud)
我目前正在工作/myproject/gui/energy/configuration/working_file.py,我想在/myproject/service/constantsnamed 下创建一个文件global_variables.json
我试过了
with open("../../../../../service/constants/global_variables.json", 'w') as file_handler:
content = json.load(file_handler)
Run Code Online (Sandbox Code Playgroud) 我有一个utils.py包含名为的函数的文件f1()。
通过另一个Python脚本,我可以import utils或execfile('utils.py')可以访问f1()。两种方法有什么区别?
我在将任何内容导入我打算使用 py.test 运行的测试文件时遇到问题。
我有一个项目结构如下:
/ProjectName
|
|-- /Title
| |-- file1.py
| |-- file2.py
| |-- file3.py
| |-- __init__.py
|
|-- /test
| |-- test_file1.py
Run Code Online (Sandbox Code Playgroud)
我无法在test_file1.py文件中使用 pytest 获得任何导入语句,因此目前我只是尝试使用声明的变量file_1.py并在test_file1.py运行时将其打印出来。
file1.py 包含:
file1_variable = "Hello"
Run Code Online (Sandbox Code Playgroud)
test_file1.py 包含:
import sys
import os
sys.path.append(os.path.abspath('../Title'))
import file1
def test_something():
assert file1.file1_variable == "Hello"
print(file1.file1_variable)
Run Code Online (Sandbox Code Playgroud)
我的测试文件中的导入语句取自/sf/answers/719104361/,它通过编辑 PYTHONPATH 变量来工作。这允许我运行test_file1.py脚本并成功执行print语句。
但是,尝试py.test从/ProjectName目录运行会给我一个错误提示ImportError: No module named 'file1'
有什么方法可以让我更好地构建事物,以便 pytest 成为可能?我需要在我的__init__.py文件中添加一些东西吗?
重复:在Python中,如何获取当前正在执行的文件的路径和名称?
我想找出当前正在执行的脚本的路径.我已经尝试过os.getcwd()但是只返回我运行脚本的目录,而不是脚本存储的实际目录.
我的书说:
在您的计算机上运行的每个程序都有一个当前工作目录或 cwd。任何不以根文件夹开头的文件名或路径都假定在当前工作目录下
因为我在 OSX 上,所以我的根文件夹是 /。当我在os.getcwd()Python shell 中输入时,我得到/Users/apple/Documents. 为什么我的 cwd 中出现 Documents 文件夹?是说 Python 正在使用 Documents 文件夹吗?没有任何以/( 根文件夹 )开头的指向 Python 的路径吗?另外,每个程序都有不同的 cwd 吗?
批处理中有一个命令%~dp0的意思是将目录更改为脚本目录,例如:批处理文件在桌面,如果您使用该目录,则无需键入即可%~dp0立即转到C:\Users\Windows\Desktop 。%~dp0有没有与Python 中相同的代码?
python ×10
python-3.x ×2
argv ×1
exception ×1
execfile ×1
file ×1
import ×1
importerror ×1
macos ×1
path ×1
pytest ×1
python-2.7 ×1
shadowing ×1
windows ×1