try/except block and ctrl-d/EOFError - python

I'm trying to learn Python.
I'm using Windows 10 on a desktop PC.
I initially went through University of Michigan's 'Python for Everyone' on the edx.org platform. For that I used the Atom editor and installed Python from www.python.org
Installed version of Python is 3.10.4.
I then uninstalled Python and Atom and installed Anaconda/Spyder. The reason for doing this was that I wanted to make use of the tools from JPL/NASA for calculating and visualizing orbits (as in orbital mechanics; search for 'AWP orbital' on Youtube, you'll see what I wanted to do). I tried initially to install the required libraries, but as I had read in a post on this site, attempting to do this in Windows 10 will probably not work, so the recommendation was to use Anaconda as it comes with literally everything. So after switching to Anaconda, I was able to run the samples that that Youtuber provided in his github, but I didn't understand enough to do anything else with it.
So, having realized that I'm in way over my head, still need to do more basic stuff in Python. I'm 47 so this stuff doesn't come easy to me. No prior programming experience other than messing around with C a bit in uni 20+ years ago to do number crunching.
Sorry for rambling on... in any case, now I'm going through the Harvard CS50 Introduction to Programming with Python on edx.org and I uninstalled Anaconda and went back to Atom and installed Python from python.org like the first time around.
Finally I'm getting to the question. Before installing Anaconda, when I used cmd or the powershell, I could use the ctrl-d to get the EOFError in a try/except block of code, but now when I use either cmd or powershell, ctrl-d just displays ^D but doesn't actually execute the break that I have after my except statement. I tried changing it to ctrl-c and changing the except to KeyboardInterrupt, and that does execute the break, but it seems it doesn't work quite the same way. It would seem that having switched to Anaconda and then back to a basic install from python.org is what caused this ctrl-d to not work anymore, but I don't have a clue as to what I need to do to restore this functionality.
Any ideas?

Related

Can't get started with VSCode and Python 3.9

I'm embarrassed to ask this, because I consider myself a seasoned programmer in general and with Python in particular (albeit mostly on Linux), but I just can't seem to get VSCode and Python working together. This is all on Windows 10.
I seem to have both installed. I've done my best to follow instructions at
https://learn.microsoft.com/en-us/windows/python/beginners
and I can start python as "python" or "python3", see that it's version 3.9.6, and can get it to say "Hello World!". However, I cannot follow instructions to start python as "py" because it reports no such animal.
I have VSCode. It says I have "Python for VSCode" when I list Extensions.
But the stopper is that a few steps into the instructions at
https://code.visualstudio.com/docs/python/python-tutorial#_start-vs-code-in-a-project-workspace-folder i get stuck because when I attempt to select a Python interpreter,
it says there is no such.
I've tried a bunch of stuff, but since it's not clear what's wrong or missing, it's hard to know if I've tried everything. Except that clearly I haven't.
Can someone guide me a bit?

I/O Error: Bad File Descriptor, can not save python file

I recently uninstalled and reinstalled python, and i have not been able to save one of my programs since.
When i hit ctrl+S, IDLE throws me a window saying I/O Error: Bad file descriptor. I can not even save my file!
As it turns out i don't think it has anything to do with the actual code. No matter what is in the program, it still throws this error when i try to save, unless there is no code whatsoever!
IF anyone knows why this error is occurring, please tell me or post an updated version of the code, any help is appreciated
I am using Windows 10, Python 3.7.3 64-bit [a couple days ago i uninstalled (just through windows settings) 32-bit and installed 64 from the python website]
I have experienced the same issue.
In my case the Windows 10 Defender was the root cause.
I added in Windows Defender Ransomware Protection the python.exe of my used IDE and the issue disappears.
In Windows, it is theorically possible to install 32 bits and 64 bits versions of Python side by side, and it should work with a genuine installation. But dragons are waiting around:
it is possible to have shortcuts pointing to a wrong location.
if the PATH has been changed to allow direct usage of the python, or pip command from the command line, risk is that you use the wrong tool
if any Python environment variable has been set, problems are almost guaranteed
Furthermore, Python can be installed either for the current user or for all users, which adds more possibilities for inconsistancies.
Once an installation is deemed broken, uninstalling one of the versions is generally useless on can even cause more problems. Long story short, if you have entered the world of inconsistancy, you must clean up everything.
My advice here is:
find where the Python versions were installed and note it
find if additional tools (py) have been installed and try to find which ones
uninstall every Python version
control that the installation paths are empty
search the environment and PATH for any Python related information and remove them
When everything looks good, reinstall from the installation wizard.
Hopefully it should work. If it does not I cannot help: despite being presented as an end user friendly system, Windows is a very feature rich and complex OS and trying to fully analyze a Windows system is beyond the capacity of most users, including most power users and sysadmins. At a point, the only possibility left is to reinstall the full OS and then cleanly install everything back... when it is possible...

Python - C Compile errors - gcc failed

Oftentimes when I try to install a Python package, installation will fail with following error: "error: command 'gcc' failed with exit status 1".
I am at loss. Most things I try to instal fail. I am losing my mind on this. Really like the language, but this is driving me crazy. Spent countless hours looking for solutions, to no avail.
Probably the problem comes from my environment. Here's how I am (insanely?) trying to run things:
Windows 7 machine
MS Visual C++ 14 redist freshly installed
Python 2.7.15 through Jupyter/SageMath 8.3 (can't execute Python through Console because throws 16bit error)
P.s.: I'm an ignorant, but I know it, so don't bother. Any help appreciated.
Ok, I found a workaround/solution.
I am suspecting that the Python kernel provided with SageMath is not a 100% complete/functional Python kernel/core. I reluctantly conceded to install a standalone Python "kernel" (not certain the term is technically correct) from Python.org, v. 2.7.15, using the standard prebuilt binaries for x64 Windows systems. Installed it in a different folder than Sage of course, added it to the system Path variable, and guess what? Yeah, issues resolved. Everything now installs flawlessly - well, mostly, but at least now it's down to an acceptable level of failure. So I installed Jupyter on this kernel and I can now continue my development the way I like it.
I mean, I would if a few hundred lines of code weren't too many for the computer on which I am working... Since it's lagging a bit, I will need to resort to other 'IDE' solutions. But that's another story altogether...
Thanks much nonetheless to everybody who bothered dropping a line by. Especially #Red Cricket, who helped me reconsider my reluctancy to install another Python kernel alongside SageMath's. Not a big fan of duplication, but hey, enough time lost there.

How can I get auto-indentation to work with Python 2.7 on Xcode 7.3?

I've used this guide to set up Xcode to build Python scripts. When I press return, the cursor always goes back to the beginning of the new line, even though automatic indenting based on syntax is turned on in the preferences menu. It's frustrating to have to press the space key eight times to get two blocks in indenting in every time. Is there something I haven't installed correctly? I have Anaconda installed and as my build tool if that helps.
I would suggest running Python on something else imho. Something like PyCharm or IPython. The problem with Xcode is that Python is not one of the native languages for it. So Xcode doesn't know the proper formatting for Pythons syntax.
Not to mention that debugging and other features might not work well on Xcode.

Python game design without Pygame

This topic is in response to a problem I've been having getting Pygame to be set up correctly on my computer. I have Windows 7, Python 3.3 and "64-bit" Windows (now I know what a bit and a byte are, but I don't really understand the implications of having a "64-bit" computer) I'm proficient in Python but know nothing about binaries, dependencies, registries or other such internal/system-level structures; not that I'm not interested, but at the moment most of what goes on is essentially "over my head".
But anyway, with that said, the problem I've been experiencing is basically that I can't install pygame. I've been over several versions of the same question (on Stack Overflow and other places on the web), and I've attempted to install pygame countless times. Sometimes it seems to work fine until I attempt to use it (the installation appears to have been successful but Python gives me errors when I try to use pygame), or I get something about Python 3.3 not being in my "registry" (which from what I read appears to be another OS-level/internal structure for those who understand "the base code of the universe"). They could actually develop a series of tutorials on just the installation process. :)
But all joking aside, I am at a loss here and considering just giving up on pygame. So my question would be, is there any way to use what's already installed (Python's libraries etc.) to develop games? If not, do you know of any alternatives that don't require the same level of experience to install? I've Googled around but everything I've found about game design in Python leads back to pygame. Thanks in advance.
To save yourself pain, just use 32bit python, and 32bit pygame. If you mix 32 and 64 bit, it will not run correctly. That's probably the problem you're having.
Since you have python 3.3, you would use https://bitbucket.org/pygame/pygame/downloads/pygame-1.9.2a0.win32-py3.3.msi
You shouldn't have to edit the registry or your environmentvariables unless something goes wrong.
As mentioned previously, tkinter would offer a solution. For example, this was created in tkinter (http://www.youtube.com/watch?v=RHxLkNryOzI)
The pygame downloads page hasn't been updated for a while and is incompatible with python 3.x. Newer versions of pygame can be downloaded here.
I'd personally use pygame as there's audio support as well but tkinter could be used.

Categories

Resources