Need Python guide on Windows - python

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.

Related

How to I force MS Visual Code to use python 2.7

I have been running Visual Studio code on the Mac for about 2 years now and I'm running into issues that I haven't seen before. I have not personally run any updates for Code for quite a while. lets say late November.
1) A message at the lower right of my Work space that says
The macOS system install of Python is not recommended,
some functionality in the extension will be limited.
Install another version of Python for the best
experience.
Unfortunately I can't update to Python 3.x and it shouldn't be up to Code to force me to update. Is there a way to turn this message off?
2) Related to above is that some classes or language keywords (JSONUtils, #unittest, requests, def) are no longer being recognized. Some constants that I have created and a variable defined to store a class.
sc = SomeClass()
This will be recognized at definition but later during usage it will not be recognized.
sc.SomeMethod( 1, 2, 3 )
sc won't be recognized. None of this is making sense to me as it is not a pure pattern. Everything is probably the same issue. Need to point Code to python 2.7.
This is a bug in VS Code. A new future version will have an ignore button.
VS Code Issue 4448
I still can't comment, so here it is an an answer instead...
This looks like a duplicate of How can I change python version in Visual Studio Code?
The short version that worked for me:
CTRL+SHIFT+P to open command palette
Choose "Python: Select Interpreter"
Point VS Code to the Python interpreter you wish to use.

Python Repl in Python

Am I right in assuming that the Python shell is coded in Python? If so, where, might one find the source code? If I wanted to write a shell program that did things differently, could I just copy the code to new_shell.py, make a few changes, and then set up a bash alias 'python'='python3 new_shell'?
There are various versions of Python. The most popular one is cpython, and the source code for that can be found here: cpython. If you want Python written in Python, check out PyPy.

Python Input EOF Error in Sublime Text 3 [duplicate]

I'm trying to run C code in sublime text 2 and i've noticed that scanf seems to be completely ignored, although it works fine in both xcode and in the terminal with gcc.
I personally prefer the look of sublime text, is there a way to fix this?
This has been discussed in several questions on SO, and the unofficial docs are currently being updated to more clearly state this: neither ST2 nor ST3 supports direct input to programs running within build systems inside Sublime Text: C/C++'s scanf and colleagues do not work, nor do Python's raw_input (Python 2) or input (Python 3), Ruby's gets, Java's Scanner class, etc. This is a fundamental limitation of the program.
However, it can be worked around in two ways. First, you can run your build system in a terminal. Second, if you are using an interpreted language like JavaScript, Python, Ruby, Perl, Scheme and all the associated Lisp-like languages like Clojure, Erlang, Haskell, PHP, R, etc., you can check out the excellent SublimeREPL plugin. You can open a REPL for interactive programming, and send programs (or bits of them) to it and interact with them just as on the command line. Documentation is here.

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.

Check if code works on newer python versions

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.

Categories