Learning Python the Hard Way terminal setup - python

So I'm trying to setup the terminal in conjunction with Notepad++ and I'm a little confused. I've managed to download Python 2.7 and load it up in Powershell but I don't really know where to go from there. Do I even need to use a terminal? I know it says you can't use IDLE but it would be a lot easier. If anyone can guide me through getting the right setup to go on with the book I'd appreciate it a lot.

If you managed to get python running in the powershell following the Exercice 0, and you read the "Warning For Beginners" section, you can proceed to Exercice 1.
Why do they recommend that setup? Notepad++ is on Windows a powerful text editor very similar to other text editors, so you will soon get familiar with it. You will need to save your scripts in files and type a special line in the powershell to run and test them. That way you will get accustomed to running programs with arguments and if you make a failure, you can edit your code, save it and run it again.
In IDLE, you are already in a Python environment, so you cannot give arguments to your program inside it (actually you can but it's complex). You can also get an editor, save your file and run the script but also here it's a harder and less obvious way than Notepad++. When you get comfortable with the environment in that book you may change to get something that suits better your way of doing.

Related

How do I stop Python from running code right after I type it in?

I just started to code on Python, so I downloaded the "Python 3.10 (64-bit)". The problem is that it runs the code without my permission. For example, if I am trying to type in print("example") it just runs on it's own. This means I can't do an actual coding project, it just runs the 1 piece of code right after I hit the Enter button. I don't know if I'm just blind or it's obvious, but I can't figure it out. What do I do?
Hitting enter after you type is "giving permission" to run the code that is typed (or copied)...
You can still write "scripts" this way - you define variables, functions, classes, etc, and they remain in scope of the interactive session.
If you want to "code a project," though, you should use an external IDE such as Visual Studio Code, PyCharm, Spyder, etc. The built-in IDLE is very basic, but still does allow for file execution.
Where do you write your code?
If you are using the command line, you cannot do anything to change this; you have to use a file with the extension ".py", for example "first_file.py", and write the code inside that file. When you are done or when you want to test your code, you have to open the terminal in the file folder and type python [filename].py and after that, your code will be compiled and executed in the terminal. I recommend using a code-writing program like VS Code, Atom or something like that.

Are there any other ways to share / run code?

So I just created a simple script with selenium that automates the login for my University's portal. The first reaction I got from a friend was: ah nice, you can put that on my pc as well. That would be rather hard as he'd have to install python and run it through an IDE or through his terminal or something like that and the user friendliness wouldn't be optimal.
Is there a way that I could like wrap it in a nicer user interface, maybe create an app or something so that I could just share that program? All they'd have to do is then fill in their login details once and the program then logs them in every time they want. I have no clue what the possibilities for that are, therefore I'm asking this question.
And more in general, how do I get to use my python code outside of my IDE? Thusfar, I've created some small projects and ran them in PyCharm and that's it. Once again, I have no clue what the possibilities are so I also don't really know what I'm asking. If anyone gets what I mean by using my code further than only in my IDE, I'd love to hear your suggestions!
The IDE running you program is the same as you running your program in the console. But if you dont want them to have python installed (and they have windows) you can maybe convert them to exe with py2exe. But if they have linux, they probably have python installed and can run you program with "python script.py". But tell your friends to install python, if they program or not, it will always come in handy

I need help recognizing files in komodo in order to run them in python

I am just learning python as my first programming language, and I just installed python 3.3, 64 bit on my windows 7 OS.
I installed komodo edit 8.0, and I am trying to print ('Hello world'). I set up the correct path so that I can access python through my command prompt.
From komodo, I saved my helloworld.py file to my desktop.
When I try to run the command prompt, I search for the file, and it says file not found, or file does not exist. I can open the folder from komodo, but it appears that it is empty. When I open the folder directly from my desktop, I see the file is in there, so it seems that komodo is not recognizing it.
How can I get Komodo to recognize my saved file and run it in python? I am very new so please go step by step if you can.
Thank you!
Many of the comments you've received recommend avoiding Komodo IDE. But you're not using Komodo IDE! You are using Komodo Edit, so the comments about IDEs just don't apply.
Sure, you could use Notepad++ or even plain old Notepad, but neither of those offers any real benefit over Komodo Edit. In fact, you would be losing a valuable feature of Komodo (both IDE and Edit version): realtime syntax checking.
I use Komodo IDE and like it quite a lot. If you want to run Python programs inside Komodo and debug them right there, Komodo IDE is a great choice.
One problem may be simply that you're expecting Komodo Edit to offer the same features as Komodo IDE. It doesn't. It's just a very nice editor. You need to run your Python code outside Komodo using the command line or other means. Just open a command prompt, cd to your directory, and type python yourfilename.py.
But you say you already tried that? It must be simply a matter of being in the wrong directory.
If you have your .py file open in Komodo, do a Ctrl+O right there, and the Open File dialog will show you the file's path at the top of the dialog. You can use Alt+D to select the path, and from there you can copy it and paste it into the command line if you need to cd to that directory.
If you'd like to use an interactive debugger (an excellent idea!) without paying for Komodo IDE, you can use PythonWin.
Install Mark Hammond's Python for Windows extensions. Included in this package is PythonWin. You can open your .py file in PythonWin and select File/Debug/Step In to start debugging your code. Then you can use the other commands on the File/Debug menu to step through it. Take note of the F10/F11/etc. keyboard shortcuts which give quicker access to these commands.
Whichever way you do it, I highly recommend using an interactive debugger like this when learning a new language. Being able to stop the code and look at your variables right then and there is a huge improvement over being limited to print statements for debugging.
So I disagree quite strongly with the recommendations against using an IDE like Komodo. The very first thing I look for when I learn a new programming language is an interactive visual debugger. I don't care too much whether that debugger comes packaged as part of an "IDE" or is a standalone debugger, as long as it shows the source code, makes it easy to single step, and shows the variables whenever it's paused.
Ok heres what I personally do.
Open run, type in cmd
Navigate to whatever directory my mypythonfile.py file is
Open whatever text editor you feel like ( personally i use notepad++ because it is NOT an IDE like kodomo, but just a pretty text editor.)
Type python mypythonfile.py and hit enter. This will run the program.
Open mypythonfile.py in text editor program.
Make changes to the python file.
Go back to the cmd window and press arrow up ( to go to the last typed command) and then press enter again, to run the program again.
Repeat steps 6-7 until your program is perfect.
It seems like you are having trouble with the Kodomo IDE instead of the actual learning python process. IDEs are complicated tools with lots of buttons that are scary. Learn the language first, then once you are comfortable there, then maybe you will use an IDE? Or maybe you will just keep using a text editor instead. Thats up to you.

python programming query

I'm new to Python programming.
My question is where to write python programs in linux--
in terminal or
any writing pad like gedit etc
Should we write one program again and again for running or we should call the program and run it.
After you install Python (it's installed by default on most Linux-es), you have two options:
Terminal. Just type python <ENTER> and you will be taken to the Python interpreter interactive prompt. For anything but the most basic stuff, however, you may want to intall IPython - an alternative interactive prompt that's much better than the default one.
In a file. Save your code into a .py file and run it with python myfile.py
But first and foremost, start by learning Python - the official tutorial is a great place to start. Among other useful information, its chapter 2 discusses how to use the Python interpreter for beginners.
Stackoverflow also has a lot of great resources for learning Python. This question, for example, and many others.
If you are learning, or you are evaluating expressions, you could run Python in terminal, or use IDLE.
But if you are writing large chunks of code, then you should consider using an IDE.
You could either use Geany, or use Eclipse with PyDev. I prefer Eclipse myself.
For running, you can run it using the command python program.py, or just add the line
#!/bin/python
to the beginning of your program, grant it execution permission using chmod, and run it with ./program.py command.

How can I save / copy classes & functions I've written in the python interpreter?

How can you save functions/ classes you've writing in a python interactive session to a file? Specifically, is there a way in pydev / eclipse's interactive session (on a mac) to do this?
I just started learning python - and am enjoying using the interpreter's interactive session for testing and playing with modules I've written. However, I find myself writing functions in the interpreter, which I think, oh it would be cool to save that to my script files. How do I do this?
I tried:
import pickle
pickle.dump(my_function, open("output.p", "w"))
But it seems to be more of a binary serialization, or at least nothing that I could copy and paste into my code...
Are there ways to see the code behind classes & functions I've defined in the interpreter? And then copy them out of the interpreter?
Update:
Ok, here's what I've learned so far:
I missed the easiest of all - PyDev's interactive session in eclipse allows you to right click and save your session. Still have to remove >>>'s, but gets the job done.
IPython is apparently the way to go to do this.
How to save a Python interactive session? has more details.
The best environment for interactive coding sessions has to be IPython, in my opinion. It's built on and extends the basic Python interpreter with a lot of magic, including history. For example, you can issue the command %logstart to dump all subsequent input to a file, which still needs to be edited afterward before it will be a script, but gives you a lot to work with.
When installing IPython, don't forget pyreadline.
In general, however, it is best to write code in an IDE and then run it. IPython helps here as well. If you write and save the script, then use the IPython "run" command to run it, the entire global namespace of the script will be available for inspection in your IPython session. Additionally, you can use the -d argument to run to trigger the pdb debugger immediately on any unhandled exception.
If you're more of a straightlaced IDE and debugger kind of guy, then the easiest and best lightweight environment has to be PyScripter.
I think the answer is to change your workflow.
What I do is write my functions in an editor (emacs), and then press a key combination (Ctrl-c Ctrl-e) to send the region of text to the (i)python interpreter.
That way I can save the function if I want, and also play with it in an interpreter.
Emacs is central to how I do it, but I'm sure there must be similar approaches with many editors (vim, gedit, etc) and IDEs.
PS. Finding a good editor is crucial when working with Python. The editor must be able to move blocks of code to the left and right easily, or the whitespace issue becomes too onerous.
I dislike typing blocks of code in the python interpreter because it doesn't allow me to shift blocks easily. You'll like Python even more when you find the right editor.
You can setup a python history file which stores everything you type into the interpreter.
Here's how:
http://docs.python.org/tutorial/interactive.html
I think it can't be done.
Python can perform instrospection with the inspect module, but the inspect.getsource function won't work without a source file.

Categories

Resources