is `python3` always installed with Python 3? - python

I usually start all my scripts with the shebang line
#!/usr/bin/env python
However our production server has Python 2 as the default python, while all of our new scripts and programs are being built under Python 3. To help keep people from accidentally running the script with the default Python 2, I am considering switching all my shebangs from now on to this;
#!/usr/bin/env python3
On our server, python3 indeed points to Python 3, and our basic scripts will run correctly on it. However I am not clear if this is something specific to our installation, or if python3 is always available if Python 3 is installed?
I know this probably will not help a user who runs $ python myscript.py when the default Python is loaded, but its better than nothing and is clear enough in letting a user who inspects the script realize they are using the wrong Python version. Though now I also realize that, with Python being on version 3.8, a Python 4 is imminent... at the same time, I am not sure I am ready to embed code in every single script to check if Python >= 3 is loaded...

Yes, this is a safe bet.
PEP 394 recommends Python 3 be available under the binary name python3 and most Linux distributions follow this recommendation. In fact, this is the only name under which Python 3 has been available in most distributions (the only outlier being Arch Linux, but even that also provides a python3 binary), and plans to make the ‘plain’ python binary also refer to Python 3 have only been made quite recently. The article ‘Revisiting PEP 394’ on LWN.net has more details.

I believe that a python 3 version only install's python3 if there is already another version of python installed, no matter if it is a python 2 or python 3 version, because the standard python command would then not work properly for the new version of python.
But please correct me if I'm wrong!

Related

How does python handle different python versions for code & imported custom libraries [duplicate]

How to use python 2 packages in python 3 project?
I have a Python 3 project, but I need some packages which are written in Python 2.
I do not want to rewrite these python-2 packages, so forking / 2to3 is not an option.
A lot of the previous questions concern using Python 2 modules in Python 3 projects. Here's the most complete explanation. It also works the other way around and suggests the following alternatives:
Using the subprocess module for requests and getting the response through the CLI. So far the most straightforward way to make cross-references.
python-fire can quickly add a CLI to a Python 3 library. You'll have a reasonable API to call from the Python 2 project.
3to2. This utility converts Python 3 code to Python 2. It remains unclear how it handles complex Python 3 projects with dependencies and Python 3-only features.
six and __future__. It seems both of these modules are intended for writing the code compatible with Python 2-3, not for using Python 3 libraries in Python 2 projects.
You can't. Any module you import in py3 codebase needs to be py3 compatible. If you can't make the upstream project do it for you, you'll have to do it yourself. As mentioned in the comments, 2to3 utility should help you with that.

I have to upgrade my application python version from 2 to python 3

I have to upgrade my application's python version from 2 to python 3.
how i can check all python 2 library dependencies. Whether it will break any libs dependencies in python 3.
any suggestion over this will be appreciated.
Currently checking with -
Python2to3- Code fixer used to correct language syntax and annotator.
Pip check - collects all libs and its version dependency.(Not working for me in server)
CanIusepython3- Python module.
pipdeptree - But how it will tell me whether package version compatible with python3 or not.
You could try using this library:
https://docs.python.org/2/library/2to3.html
If it does not do a well good of a job, you can always try manualy. It's an incremental process so you would need to change things slowly.
Used modules
An automated tool can probably not give you complete information, for several reasons;
Some modules have changed name or usage between Python 2 and Python3 compatible versions.
Some modules might not have been updated for Python 3, but alternatives exist.
So I would suggest the following. Using e.g. egrep, extract all the import statements from the applications code. I would use something like this (from the source code directory):
egrep -Rh '^import .+|^from [^\.].+ import' . | sort | uniq
If your operating system doesn't support these tools, you can easily write a small python script to extract import statements from all your code files.
Once you have a listing of all imports, disregard modules that are part of the standard library or that are part of the application.
For the remaining external modules you will have to search online and check if there is a Python 3 compatible version or alternative.
Converting your code
As mentioned, 2to3 can do most of the changes that can be automated for you.
For the rest I would recommend using a good code checker like e.g. pylama and an automatic code re-formatter like e.g. yapf.

Using Python in NetBeans 8.1

I'm trying to add Python functionality to NetBeans 8.1. I downloaded Python from the Python website here. (I'm using Windows 10, by the way.) I also downloaded the NetBeans plugins "Python" and "Jython Distribution" (required by the former). Now I have two questions:
1.
When I create a new Python project, I have these options:
Based on the description for the "Setuptools" choice, I'm guessing that I shouldn't use that one since it's not for Windows. So am I correct in choosing "Python Project - Ant"?
2.
The Python platform that is selected by default is "Jython 2.7.0," as shown below.
I'm assuming that what I got from the Python website was a Python platform too, so I'd like to use that if possible. But in trying to add another platform, I don't know what file to choose in my Python folder. All of the ones I've tried result in the error:
I guess I could use the Jython platform, but I would think that the stuff I downloaded straight from Python would be the best. Do I need to add and make default the stuff I downloaded? Or should I ignore that and just use the Jython platform?
If yes to "should I add a platform", what file do I select?
Thanks in advance for any help.
EDIT:
I was able to create and run successfully a Python project using the "Python Project - Ant" option as mentioned in question 1, using the Jython platform. So question 1 is taken care of. I still would like to be able to use the Python platform I downloaded from their website, though, so question 2 still needs an answer.
For those who are trying to run other python distributions from netbeans, here is what i found:
From python wiki - https://wiki.python.org/moin/IntegratedDevelopmentEnvironments
Python/Jython support in NetBeans -- Open source, allows Python and Jython Editing, code-completion, debugger, refactoring, templates, syntax analysis, etc.;
UPDATE: Netbeans 7.0 released without Python support. Check
http://wiki.netbeans.org/Python70Roadmap for upcoming Python support.
So it looks like, python (Cbased) will no longer be suported in NetBeans, only Java-based Jython - also look on this question:
What is the difference between Python vs Jython vs IronPython vs wxPython?

Running multiple pythons

I recently upgraded to python 3.4 to use continuum tools but many of my scripts are written for 2.7. This can causes some errors; some are simple (like "print" now requires parentheses), but others are more complicated:
if struct.unpack("h", "\0\1")[0] == 1:
defs.append(("WORDS_BIGENDIAN", None))
Yields the error:
File "setup.py", line 302, in build_extensions
if struct.unpack("h", "\0\1")[0] == 1:
TypeError: 'str' does not support the buffer interface
Is there a way to run my python code as 2.x like you can with C++ (-std=c++11 etc) ? It's possible that many more errors will surface if I just solve this one. Thanks!
If you have several versions installed, you can change the first line of your python script to explicitly use 2.x or 3.x:
For a python 2.x script:
#!/usr/bin/env python2
or, for a python 3.x script:
#!/usr/bin/env python3
Python 3 is really a different language than Python 2. There's no way to make the Python 3 interpreter run Python 2 code (unless that code doesn't happen to use any of the features that were changed).
You may want to read the guide to porting to Python 3 in the Python documentation. Here's a brief summary of the current recommendations:
If you only need to support Python 3 from now on (you don't need to maintain Python 2 compatibility), use the 2to3 tool to translate most of your code, then manually fix up whatever it has missed. There is lots of documentation that explains the changes between versions, if you haven't used Python 3 before.
If you're writing new code and need to be able to run it with both Python versions, write for Python 3 (or a common subset of 2 and 3) and backport to Python 2 as necessary.
If you have an existing Python 2 codebase and you want to run it on Python 3 without breaking Python 2 compatibility, use libraries like six and from future imports to help you port your code to a common subset of the two versions of Python. 2to3 and other tools like modernize will help you find the places you can improve things. Note that it's easier to make this work if you drop support for older version of Python 2.

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