当我在python 2.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
Run Code Online (Sandbox Code Playgroud)
代码是:
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']
)
Run Code Online (Sandbox Code Playgroud)
Nil*_*esh 125
当您os.path.join(os.path.dirname(__file__))在python交互式shell中追加此行时,会出现此错误.
Python Shell不检测当前文件路径__file__,它与filepath您添加此行的路径相关
所以,你应该写这条线os.path.join(os.path.dirname(__file__))在file.py.然后运行python file.py,它的工作原因是它需要你的文件路径.
Kwu*_*ite 15
我遇到了与PyInstaller和Py2exe相同的问题,所以我在cx-freeze中遇到了常见问题解答.
从控制台或作为应用程序使用脚本时,下面的函数将为您提供"执行路径",而不是"实际文件路径":
print(os.getcwd())
print(sys.argv[0])
print(os.path.dirname(os.path.realpath('__file__')))
Run Code Online (Sandbox Code Playgroud)
资料来源:http:
//cx-freeze.readthedocs.org/en/latest/faq.html
你的旧行(初步问题):
def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
Run Code Online (Sandbox Code Playgroud)
用以下代码替换您的代码行.
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,您可以将应用程序添加到操作系统的路径中,您可以在任何地方执行它,而不会出现您的应用程序无法找到它的数据/配置文件的问题.
用python测试:
小智 15
如果您在 .py 文件中使用代码:使用
os.path.abspath(__file__)
如果您直接在脚本上或在 Jupyter Notebooks 中使用代码:将文件放在双引号内。
os.path.abspath("__file__")
Elo*_*ims 10
你在使用互动翻译吗?您可以使用
sys.argv[0]
Run Code Online (Sandbox Code Playgroud)
您应该阅读:如何在Python中获取当前执行文件的路径?
lsh*_*eng 10
我通过将文件视为字符串来解决它,即放入"__file__"(与引号一起!)而不是__file__
这对我来说很好:
wk_dir = os.path.dirname(os.path.realpath('__file__'))
Run Code Online (Sandbox Code Playgroud)
小智 7
如果您要查找的只是获取当前工作目录os.getcwd(),os.path.dirname(__file__)那么只要您没有更改代码中其他位置的工作目录,就可以获得相同的功能. os.getcwd()也可以在交互模式下工作.
因此
os.path.join(os.path.dirname(__file__))
变得
os.path.join(os.getcwd())
小智 7
如下更改您的代码!这个对我有用。`
os.path.dirname(os.path.abspath("__file__"))
Run Code Online (Sandbox Code Playgroud)
如果你从 python shell 运行命令,你会得到这个:
>>> __file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
Run Code Online (Sandbox Code Playgroud)
您需要直接执行文件,将其作为参数传递给python命令:
$ python somefile.py
Run Code Online (Sandbox Code Playgroud)
在你的情况下,它真的应该是 python setup.py install
I've run into cases where __file__ doesn't work as expected. But the following hasn't failed me so far:
import inspect
src_file_path = inspect.getfile(lambda: None)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
144522 次 |
| 最近记录: |