简体   繁体   中英

Can I install Python 2 packages in Python 3 using pip?

On my Python 3.6 installation, I just tried

pip install nipet

but I get this:

Complete output from command python setup.py egg_info:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\me\AppData\Local\Temp\pip-install-eef9zqvc\nipet\setup.py", line 64
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('e> the current operating system is not supported.')?

This is most probably related to nipet targeting Python 2.x.

I have successfully tried 2to3 on some part of this package, and noticed that all incompatibilities come down to print statements. However, since it is setup.py which fails, I cannot even install all the files in their proper location to run 2to3 on.

Is there some kind of pip wrapper for 2to3 that would allow me to install a Python 2.x package without much manual effort?

TL; DR: No, you can't.


nipet is not Python 3 compatible. As @deceze said in comment, you need to fix that before trying to install it. pip can't do the job.

Checkout the code and make it Python 3 (this may involve 2to3 and probably manual changes). Then retry to install it. If you're happy with what you got, you may submit your Python 3 port to the maintainer.

Also, nipet should be fixed to specify it is Python 2 only so that it is listed as Python 2 only on PyPI and pip3 does not even attempt to install if. You should open an issue in the bugtracker to ask the maintainer to fix that. Or even send a pull-request adding appropriate classifiers to setup.py, If you're comfortable doing that.

While it is true what @Jérôme writes, this is not true for all components. For the sake of completeness, I would like to mention the use_2to3 option of setuptools , which do most of what I wanted. (They do not touch setup.py, though, it seems).

Here is some more information:

Setuptools provides a facility to invoke 2to3 on the code as a part of the build process, by setting the keyword parameter use_2to3 to True, but the Setuptools project strongly recommends instead developing a unified codebase using six, future, or another compatibility library.

https://setuptools.readthedocs.io/en/latest/python3.html

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