Check if code works on newer python versions - python

I have some rather large code I've written and tested to work with the 2.7.3 version of python available in my elementary OS 0.2 system (based on Ubuntu 12.04)
My code is hosted on Github and I'd like to know if it works on newer versions of python up to 3.x.
Is there some way to do this automatically or with as little hassle as possible?

It's unlikely that your code will work automatically unless it's some very simple script because many of the standard Python operators and methods changed. For example, the print statement becomes the print() method, and that will become a syntax error when you try to run the code.
If you're willing to take the time to set it up and install other version of python, you can use PyEnv to install multiple versions of Python to test your code with. It's very similar to Ruby's RVM if you've used that. While it's not an automated method of testing if your code will work, and it does require some setup, it is a way that you could test your code on multiple versions of Python. Once it is setup, you can continue to use it for your other Python projects as well.
In addition to PyEnv, you can take inspectorG4dget's advice from the comments and use Python's automated version 2 to version 3 code translation. Since it is unlikely that your code will work immediately in version 3 due to changing syntax of keywords and standard methods, you can use that tool to translate your code without too much effort.

Related

What versions of Python will work in Windows XP?

I would like the most advanced version of Python that still works on Windows XP. I need both Python 2 and Python 3.
What versions of Python will work on Windows XP?
I found that Python 2.7.9 and Python 3.4.4 are the newest versions of Python that work in Windows XP. I found this out through trial and error.
I've found a desperate guy (Daniel Pistelli) who didn't give up at the windows error dialog:
https://ntcore.com/?p=458
I like how he approaches the topic:
If we try to start any new application on XP, we’ll get an error
message informing us that it is not a valid Win32 application. This
happens because of some fields in the Optional Header of the Portable
Executable.
[...]
Fortunately, it’s enough to adjust the fields in the executable we want to start (python.exe), there’s no need to adjust the DLLs as well. If we try run the application now, we’ll get an error message due to a missing API in kernel32. So let’s turn our attention to the imports.
And then he goes on for another 5-6 screens. To sum it up, apparently you'll have to:
Modify the python.exe header,
Run a tiny Python script to remap some DLL calls,
Recalculate some binary hashes,
Create a fake kernel32.dll with certain calls forwarded,
Modify python37.dll to point to this fake thing,
and then you're all set. To be honest I couldn't quite follow (let alone verify!) some of the steps but it looks legit and there are links with Daniel's half-baked solutions for the harder parts, also a lot of Python sources explained. I will definitely give this thing a try.
In fact, here are the final scripts from his GitHub page:
https://github.com/dpistelli/xptmrt
Moral of the story: you can be crazy, but you'll never be dismantle-all-dlls-and-exes-and-open-hexeditors-and-disassemble-hashing-algorithms-and-mock-it-all-together-so-it-works-under-xp-crazy!
Someone had built Python 3.4.10, which is slightly newer than the officially prescribed Python3 v3.4.3/3.4.4 for Windows XP. Since it comes from the third-party (not the official Python website), use at your own risk,
-->Download<--
i tried 3.3.3 but i came up with an error message use 3.4.3/2.7.9 they are the only versions that work now sadly
There are several custom builds of Python >3.44 suitable for Windows XP. A poster at reddit has provided Python 3.55 here:
https://www.reddit.com/r/Python/comments/3tgi0t/python_35_x86_on_windows_xp/
Here is an XP compatible build of 3.6.15 source code only:
https://alex-free.github.io/winxp_python3.6.15/
At the MSFN forums someone has provided an XP compatible build of 3.8.1350 here:
https://msfn.org/board/topic/183741-python-3813-for-windows-xp-sp3/
Official website says about 3.4.10: https://www.python.org/downloads/release/python-3410/
Any of them, python is very platform independent. Some features might not work, but that would best be found in the documentation.

Can we run legacy python 2.7 code under python 3.5?

I'd like to upgrade to python 3.5, but I use legacy python 2.7 packages. Is it easy to run legacy packages in python 3.5? I have been under the impression that this isn't easy, but I did a few searches to see if I'm wrong and didn't come up with much.
I would expect there to be a multiprocessing package that allows standardized hand-offs between 3.5 code and 2.7 packages, allowing them to run independently under their own environments, but being somewhat seamless to the developer.
I'm not talking about converting my own code to 3.5, I'm talking about libraries that I use that won't be updated for or by me.
If you used the newer syntax supported by 2.7, e.g. around exceptions, and/or, better yet, worked with new features imported from __future__, you'll have much easier time converting your code to Python 3 (up to no changes at all). I'd suggest to follow this path first, for it can be trod gradually, without an abrupt jump to Python 3.
I suppose Python processes with different versions can interoperate, because object pickling format is compatible, and you can explicitly use a specific pickling protocol version on both sides to ensure that. I don't think multiprocessing packages on either side would be too useful, though. Consider using e.g. ZeroMQ as a more general solution.
Unfortunately there is no "nice" or automatic way of handling the processing of 2.7 code under 3.5 (that works perfectly).
You mentioned that you are concerned about libraries, not your own code - firstly, you'd hope that if they are under active development, they will be updated. If not, as you stated, then there is a possibility that they were written to be future proof. I've found some good ones are (e.g. google-api-python-client, e.g. https://github.com/google/google-api-python-client/blob/master/setup.py).
Failing that, the only way to upgrade is to fix all the syntax changes yourself. Most common ones I deal with are around 'print' and exception handling.

How to detect minimum version of python that a script required

I have built an application with python.
How can I detect minimum version of Python that my application needs?
Like Django , in that website it tells you the minimum version of python that it required (example: 2.6.6 and later).
It means I wanna tell to user what minimum version of python he should install on his system
I know this is an old post, but I've recently started a project called Vermin that tries to detect the minimum required Python versions needed to run code. It will do what you request, #Mortezaipo.
There isn't really an automated way to check what features your code is using and correlate that to specific Python versions. If your code relies on fixed bugs or additional keywords to existing methods, the version detection gets harder still.
Generally speaking, you set the minimal version based on experience and knowledge of what features you are using; you can check the What's New documentation.
If you have a comprehensive test suite, you could just run that on older Python versions; if all your tests pass you support that older version.

When Python is Updated Will I Have to Update My Program?

I'm working on my first Python-based program. I would like it to be as maintenance free in the future as possible and I was wondering if this could be a problem as Python is updated. I'm using 2.7.2 currently, but when 3 becomes standard what could happen to my program? Will it likely stop working on a system installed with Python 3 and will it be impractical to have the user install an older version of Python? I assume 2.7.2 won't be maintained indefinitely, and I wouldn't think newer versions of Python would run my program successfully. Sorry if this seems like a newb question; I'm used to working with compiled languages.
Not to stray too far off topic, but would it be better to use Lua in this case?
Will it stop working - that depends on how the program is being run. If the system has both versions installed and you ask to run against Python 2, then it'll continue to work. If you don't explicitly ask to run against a certain version, and it's not there, then it'll probably fail.
Lua offers you no solutions here - if you rely on a system-installed Lua and that Lua becomes incompatible in the future, you're stuck. Think of your scripting language as a dynamic library - if the user has the right version, they're ok, and if not, they don't, just like with C/C++ apps.
If you're deploying to Unix-like platforms, I expect they will support Python 2 for at least another 5 years, maybe 10.
If you're deploying to a Windows platform, you usually package up the relevant version of Python as part of your app.
So the problem is unlikely to be as significant as you fear.
It will take a very, very long time until Python2 will die.
Python2 code will most likely require modifications to run with Python3 (there's the 2to3 tool to help with migrating though), but with all the libs out there which are for python2 it will take years until py2 dies - so you don't really have to care about it right now. Besides that, I believe as long as enough people are using Python2, a version will be kept up to date with fixes.

Need Python guide on Windows

I am trying to set up Python on Windows 7. I haven't used this language before so it seems strange to me.
I've downloaded lastest Python release 3.2.2 from official site and upadate path variable.
However I still can't even run simplest program ever like this :
print 'Hello, world!'
It says that there is a syntax error and the last character ' is highlighted with red.
I don't know if my path variable has been set properly. Here is where I installed Python :
C:\Software\Python32
So I added such a variable : var name = PYTHONPATH , var value = C:\Software\Python32\Lib
Is there something with auto-completion and with errors/warnings details ( which line, hint what can be wrong ), for example like Eclipse or NetBeans OR should I use this installed Python IDLE GUI for delevoping or stuff like NotePad++ ?
Actually what is this Python shell for ? - I know that I can type in some arithmetic operations here and I will get results, but is it used for something more advanced ? ( Is it used when I am writting something bigger ? )
Could someone describe simple way to write and execute a program ( or script I a total beginner so I don't really know what it is going on here ) ?
In Python 3.2 you have to use print in the below manner. The parentheses are mandatory. (print became a function in Python 3)
print('Hello World')
As Venk stated, the print statement in Python 2.x has been replaced with print() in 3.x, so your statement should read
print('Hello World')
Since you're new, here some things you should know about Python versions:
Python currently comes in two flavors: Python 2.x and Python 3.x.
Python 2.x has been in development since the late '90s, so most existing codebases, frameworks, and libraries are written in this flavor of Python. Each successive version is backwards compatible, so, for example, all code written in Python 2.4 can be run with Python 2.4+. Its current revision is 2.7.2, which was released last year.
Python 3.x is considered the "future" of Python, and purposefully breaks a lot of the conventions, such as the print statement, in favor of a clearer, more explicit language. Most libraries are working to port over to Python 3.x, but since there are extensive changes in the structure of the language, most library maintainers have not yet been able to release a Python 3.x compatible version with the full features of the Python 2.x version of the library.
If you're developing now, you should learn Python 2.x; otherwise, it's recommended you learn Python 3.x.
To answer your other questions:
Python's native IDLE is an excellent IDE, but if you're looking for something more advanced, you may want to try out Eclipse's PyDev extension or PyCharm. I personally prefer PyCharm, since it doesn't keep giving me errors when I'm importing/using nonstandard Python libraries/frameworks.
In addition, Python's shell is used to interpret Python scripts (in the background) and for interactive interpreting (i.e., quick and dirty testing), and can execute code you type into it. The latter, however, is not recommended, as a single syntax error in multiple lines of code can force you to retype all the lines to fix a single bug.
Furthermore, all Python scripts end in .py, so if you can see the file extensions, you can convert a text file into a Python script, and run from shell by typing python path/to/file.py. Note, however, that you still have to write a valid Python script, or it will not run.
I suggest A Byte of Python. It'll take you through install, REPL, syntax, and the std library.
The python shell is for entering code interactively.
Try the following: http://docs.python.org/tutorial/
I've been using Python on Windows 7 for 3 years now, and I strongly recommend Notepad++ as your editor/interpreter. It is ideal for people who want to play around with the language and are learning. Notepad++ can be customized for almost any language and is free for Windows. Follow this link
and take a look at how to conveniently use Notepad++ to execute Python scripts.

Categories