当我在python2.7中运行这段代码时,我得到了这个错误:
Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\pyutilib.subprocess-3.5.4\setup.py", line 30, in <module>
long_description = read('README.txt'),
File "C:\Python26\Lib\site-packages\pyutilib.subprocess-3.5.4\setup.py", line 19, in read
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
NameError: global name '__file__' is not defined
代码为:
import os
from setuptools import setup
def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
setup(name="pyutilib.subprocess",
version='3.5.4',
maintainer='William E. Hart',
maintainer_email='wehart@sandia.gov',
url = 'https://software.sandia.gov/svn/public/pyutilib/pyutilib.subprocess',
license = 'BSD',
platforms = ["any"],
description = 'PyUtilib utilites for managing subprocesses.',
long_description = read('README.txt'),
classifiers = [
'Development Status :: 4 - Beta',
'Intended Audience :: End Users/Desktop',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Operating System :: Microsoft :: Windows',
'Operating System :: Unix',
'Programming Language :: Python',
'Programming Language :: Unix Shell',
'Topic :: Scientific/Engineering :: Mathematics',
'Topic :: Software Development :: Libraries :: Python Modules'],
packages=['pyutilib', 'pyutilib.subprocess', 'pyutilib.subprocess.tests'],
keywords=['utility'],
namespace_packages=['pyutilib'],
install_requires=['pyutilib.common', 'pyutilib.services']
)
发布于 2013-12-11 12:27:35
当您在python交互式shell中添加此行os.path.join(os.path.dirname(__file__))
时,会出现此错误。
Python Shell
不会检测__file__
中的当前文件路径,它与您添加该行的filepath
相关
因此,您应该在file.py
中编写这一行os.path.join(os.path.dirname(__file__))
。然后运行python file.py
,它可以工作,因为它接受您的文件路径。
发布于 2018-11-14 13:51:45
我遇到过__file__
不能像预期那样工作的情况。但到目前为止,以下几点并没有让我失望:
import inspect
src_file_path = inspect.getfile(lambda: None)
这是最接近于Python类似于C的__FILE__
的东西。
Python的__file__
的行为与C的__FILE__
有很大的不同。C版本将提供源文件的原始路径。这在记录错误和知道哪个源文件有bug时很有用。
Python的__file__
只给出了当前正在执行的文件的名称,这在日志输出中可能不是很有用。
发布于 2018-07-01 16:36:36
按如下方式更改您的代码!这对我很管用。`
os.path.dirname(os.path.abspath("__file__"))
https://stackoverflow.com/questions/16771894
复制相似问题