简体   繁体   English

在Python中使用3to2,得到“ python:无法打开文件'3to2':[Errno 2]没有这样的文件或目录”

[英]Using 3to2 in Python, get “python: can't open file '3to2': [Errno 2] No such file or directory”

I have installed 3to2-1.1.1 on my system (found here ), cd'd to the directory of my test file (a simple "Hello, world!" program written in 3 syntax) Typed into my command prompt: 我已经在系统上安装了3to2-1.1.1(可在此处找到),将其安装到测试文件的目录中(用3种语法编写的简单的“ Hello,world!”程序),输入命令提示符:

python 3to2 HelloWorld.py

With the following output: 具有以下输出:

python: can't open file '3to2': [Errno 2] No such file or directory

I attempted to change the directory name/path, but after looking over this post, I'm convinced it may be a dependency issue? 我试图更改目录名称/路径,但是在查看了这篇文章之后,我确信这可能是依赖性问题?

I have raised an issue on the creator's repo, and searched endlessly on the interwebs, but seem to be the only person in the world experiencing this issue. 我在创建者的仓库中提出了一个问题,并在互联网上进行了无休止的搜索,但似乎是世界上唯一遇到此问题的人。

I have tried uninstalling and re-installing just...so many ways. 我尝试过卸载和重新安装的方法很多。 Any and all help appreciated - even if you just tell me it's operator error. 任何帮助都值得赞赏-即使您只是告诉我这是操作员错误。

Extra notes: 额外说明:

  • I am running Python 2.7 (but also tried installing and running this with 3) 我正在运行python 2.7(但也尝试使用3安装和运行它)
  • I am using a Windows cmd 我正在使用Windows cmd

Windows cannot take care of shebangs. Windows无法照顾shebang。 The default association in Windows is through file extensions. Windows中的默认关联是通过文件扩展名。 However, the file 3to2 does not come with a .py extension (only a shebang). 但是,文件3to2不带有.py扩展名(只有shebang)。 As a result Windows is usually unable to determine what to do with the file. 因此,Windows通常无法确定如何处理该文件。

OP discussed the following use cases: OP讨论了以下用例:

  • python 3to2 <arg-file> . python 3to2 <arg-file> This doesn't work for me in a pip install. pip安装中,这对我不起作用。 Python throws an internal ENOENT (I/O) error. Python引发内部ENOENT(I / O)错误。 This use case is discarded. 该用例将被丢弃。
  • 3to2 <arg-file> . 3to2 <arg-file> Doesn't work either. 也不起作用。 This is because Windows doesn't know how to execute this file. 这是因为Windows不知道如何执行此文件。 Funnily enough Windows cannot find the file even though its location is in the system PATH . 有趣的是,即使Windows的位置位于系统PATH中,也无法找到该文件。 Calling where gives negative results. where调用会产生负面结果。 Windows is not able to handle filenames without extensions, somehow. Windows无法以某种方式处理没有扩展名的文件名。
  • Rename 3to2 to 3to2.py (in %PYTHON_INSTALL_DIRECTORY%\\Scripts\\ ) since it really is a python source file. 3to2重命名为3to2.py (在%PYTHON_INSTALL_DIRECTORY%\\Scripts\\ ),因为它确实是python源文件。 Python on Windows is set up to handle .py file by default. Windows上的Python默认设置为处理.py文件。 The location of the file is in the system PATH . 该文件的位置在系统PATH Try calling 3to2.py <arg-file> . 尝试调用3to2.py <arg-file> This works! 这可行!
  • Returning to the first case: After renaming, try calling python 3to2.py <arg-file> . 返回第一种情况:重命名后,尝试调用python 3to2.py <arg-file> Still doesn't work. 仍然不起作用。 Python throws an ENOENT error. Python引发ENOENT错误。

In conclusion, renaming the file 3to2.py is a hack that works. 总之,重命名文件3to2.py是可以的。 The dependent programs might not work. 依赖程序可能无法正常工作。


Don't give up just yet! 不要放弃!

So why did calling 3to2.py through Python fail? 那么,为什么通过Python调用3to2.py失败了? The answer is simple. 答案很简单。 Python only calls what you give it. Python只调用您提供的内容。 When you specify a relative path, it will only look in the current working directory of the program and no more. 当您指定相对路径时,它只会在程序的当前工作目录中查找,而不会在其他目录中查找。 When you give an absolute path, Python does the needful. 当您给出绝对路径时,Python会做必要的事情。


NOTES: 笔记:

  • I only installed the package through pip only. 我只通过pip安装了软件包 I did not test through any other methods. 我没有通过其他任何方法进行测试。
  • I tested this on a Windows 7 SP1 system. 我在Windows 7 SP1系统上对此进行了测试。 Newer versions may have better filesystem support. 较新的版本可能具有更好的文件系统支持。

I tried install it via pip on my Linux, it works out-of-the-box. 我尝试在Linux上通过pip安装它,它可以直接使用。

However, you could just use it like 2to3 . 但是,您可以像2to3一样使用它。 So type 3to2 -w HelloWorld.py in cmd maybe works. 因此,在cmd键入3to2 -w HelloWorld.py可能有效。 If cmd can't find the path of 3to2 , then you need to find it manually. 如果cmd找不到3to2的路径,则需要手动找到它。

kevin@Arch ~> cat 1.py 
print('Hello')
var = input('text')


kevin@Arch ~> 3to2 1.py
RefactoringTool: Skipping optional fixer: collections
RefactoringTool: Skipping optional fixer: int
RefactoringTool: Skipping optional fixer: memoryview
RefactoringTool: Skipping optional fixer: printfunction
RefactoringTool: Skipping optional fixer: unittest
RefactoringTool: Refactored 1.py
b'--- 1.py\t(original)'
b'+++ 1.py\t(refactored)'
b'@@ -1,2 +1,2 @@'
b"-print('Hello')"
b"-var = input('text')"
b"+print u'Hello'"
b"+var = raw_input(u'text')"
RefactoringTool: Files that need to be modified:
RefactoringTool: 1.py


kevin@Arch ~> 3to2 -w 1.py
RefactoringTool: Skipping optional fixer: collections
RefactoringTool: Skipping optional fixer: int
RefactoringTool: Skipping optional fixer: memoryview
RefactoringTool: Skipping optional fixer: printfunction
RefactoringTool: Skipping optional fixer: unittest
RefactoringTool: Refactored 1.py
b'--- 1.py\t(original)'
b'+++ 1.py\t(refactored)'
b'@@ -1,2 +1,2 @@'
b"-print('Hello')"
b"-var = input('text')"
b"+print u'Hello'"
b"+var = raw_input(u'text')"
RefactoringTool: Files that were modified:
RefactoringTool: 1.py


kevin@Arch ~> cat 1.py 
print u'Hello'
var = raw_input(u'text')
kevin@Arch ~> 

After installing on another system, I have not been able to replicate the issue. 在另一个系统上安装后,我无法复制该问题。 this makes me believe that something is wrong on my side of things. 这使我相信事情本身就是错的。 I will update here if I ever figure it out. 如果我能解决的话,我将在这里更新。

暂无
暂无

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

相关问题 使用Python 3to2修复程序集时出现问题 - Problems using the Python 3to2 fixers set Python 3to2工具-至少需要 - Python 3to2 Tool - Requires At Least python:无法打开文件'python2':[Errno 2]没有这样的文件或目录 - python: can't open file 'python2': [Errno 2] No such file or directory 无法打开文件&#39;python&#39;:[Errno 2]没有这样的文件或目录 - can't open file 'python': [Errno 2] No such file or directory python:无法打开文件“帮助”:[Errno 2]没有此类文件或目录 - python: can't open file 'help': [Errno 2] No such file or directory python:无法打开文件“C:\\Program”:[Errno 2] 没有这样的文件或目录 - python: can't open file 'C:\Program': [Errno 2] No such file or directory PyCharm 没有找到 Anaconda Python,给出“无法打开文件 &#39;python&#39;:[Errno 2] 没有这样的文件或目录?” - PyCharm not finding Anaconda Python, giving "can't open file 'python': [Errno 2] No such file or directory?" python:无法打开文件&#39;python DACscrap.py&&#39;:[Errno 2]没有这样的文件或目录…但是可以吗? - python: can't open file 'python DACscrap.py &': [Errno 2] No such file or directory… but it does? PyCharm 没有找到 Anaconda Python,给出“无法打开文件 &#39;python.py&#39;:[Errno 2] 没有这样的文件或目录?” - PyCharm not finding Anaconda Python, giving “can't open file 'python.py': [Errno 2] No such file or directory?” 当运行 docker-compose up 我得到 python: can&#39;t open file &#39;manage.py&#39;: [Errno 2] No such file or directory - When run docker-compose up I get python: can't open file 'manage.py': [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM