简体   繁体   中英

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:

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)
  • I am using a Windows cmd

Windows cannot take care of shebangs. The default association in Windows is through file extensions. However, the file 3to2 does not come with a .py extension (only a shebang). As a result Windows is usually unable to determine what to do with the file.

OP discussed the following use cases:

  • python 3to2 <arg-file> . This doesn't work for me in a pip install. Python throws an internal ENOENT (I/O) error. This use case is discarded.
  • 3to2 <arg-file> . Doesn't work either. This is because Windows doesn't know how to execute this file. Funnily enough Windows cannot find the file even though its location is in the system PATH . Calling where gives negative results. Windows is not able to handle filenames without extensions, somehow.
  • Rename 3to2 to 3to2.py (in %PYTHON_INSTALL_DIRECTORY%\\Scripts\\ ) since it really is a python source file. Python on Windows is set up to handle .py file by default. The location of the file is in the system PATH . Try calling 3to2.py <arg-file> . This works!
  • Returning to the first case: After renaming, try calling python 3to2.py <arg-file> . Still doesn't work. Python throws an ENOENT error.

In conclusion, renaming the file 3to2.py is a hack that works. The dependent programs might not work.


Don't give up just yet!

So why did calling 3to2.py through Python fail? The answer is simple. Python only calls what you give it. 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.


NOTES:

  • I only installed the package through pip only. I did not test through any other methods.
  • I tested this on a Windows 7 SP1 system. Newer versions may have better filesystem support.

I tried install it via pip on my Linux, it works out-of-the-box.

However, you could just use it like 2to3 . So type 3to2 -w HelloWorld.py in cmd maybe works. If cmd can't find the path of 3to2 , then you need to find it manually.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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