Asking for overwrite while trying to run script in ipython - python

I am fairly new to programming in python. I installed anaconda and am running iPython (the Jupyter qtconsole) v.4.3.0 and python v.3.6 on a Mac. Currently, I am trying to import a module with functions located in my home directory.
I have looked at stackoverflow and python documentation and found that it could be done with:
%run "Users/myUser/python_functions.py"
or
import python_functions
However, when I try both of these approaches, I get prompted to overwrite the file that I am running or importing:
File `python_functions.py` exists. Overwrite (y/[N])?
This is changing the previous file and not getting the functions I want to be imported.
What may explain this, and what can I do to import my module?

this is wrong but leaving it up for shame
import on ubuntu (and I'm guessing many other unix-like OSs including Mac) is a utility that saves any visible window on an X server and outputs it as an image file. You can capture a single window, the entire screen, or any rectangular portion of the screen.
My guess if you are running the import command in your console, and it's about to take a screenshot and save it over an existing file - python_functions
Before you the use the python import command, start a python interpreter:
$ python
>>>import yourfile
edit: on re-reading your question, I'm not so sure about my guess anymore, but leaving it up until you tell me I'm wrong :)

Running Jupyter qtconsole as an interpreter is likely causing the problem in this scenario. Instead using a IDE or command line interpreter will resolve it .
Since anaconda was installed, trying it with the IDE Spyder executes the code just fine without the overwrite prompt. It works on others (e.g PyCharm, Rodeo, etc.) as well.

Related

iPython automatically at launch in Visual Studio Code on Os X

I would like that iPython run automatically when I launch VSC instead of typing ipython and press enter in the terminal. The answer here How to set ipython/jupyter as the default python terminal for vscode? doesn't work as it is for windows but it shouldn't be really different. Also, is there something similar to the 'Execute' button in Spyder instead of typing %run filename ? Thanks !
I presume you mean you want to run the "Python Interactive Window" and not just an iPython console on startup
There is currently no way to run it on startup. At least no way without writing another extension that would run a command when opening a workspace. It would be simple for us to add one though. Probably a workspace setting. Can you log an issue here:
https://github.com/Microsoft/vscode-python/issues/new
For you second question, 'Execute' in spyder, we have 'Run Current File in Python Interactive Window'. This works on any python file. You can get to it through the context menu on a file or through the command palette.
Sadly the nice workflow of spyder is not provided by any official extension at the moment (as far as I know).
But you can implement the basics easily on your own by writing an extension. Even with no experience in TypeScript you can quickly build an extension which starts an IPython console as soon as you open a python file. I also managed to execute a startup script which implements the runfile method. VS Code also allows keybindings for your functions, so that you can almost work like you can with spyder.
Spyder modified the IPython terminal quite a bit though, so it won't feel exactly the same. But after all, everything there is open source so you could implement it yourself, which is what I'm trying to do in my free time.

How to save code at the python prompt in the terminal to a local file

I have just written a bunch of lines of code on the Python prompt at the terminal. Now, I want to save all those lines of code to a .py file.
I am unable to find out how to do that. The only thing that I could find on StackOverflow was this answer but it shows only how to do it in an iPython notebook. I am not using an iPython notebook. I am running the code at the command line on the terminal.
I tried to follow that answer (because just in case) and ran the %save magic command on the terminal but it gave a SyntaxError.
So, how do save it?
Thanks!
See http://blog.e-shell.org/174 . As wu explains, the python prompt is using readline, and you can import a Python library to access this.
>>> import readline
>>> readline.write_history_file('/path/to/history.txt')
You can trying using another interpreter : bpython , I belive it has what you need,check it out.
Save the code you've entered to a file.
You seem to be affected by the misconception, that the python environment is workspace-centered (similar to what I know from Smalltalk and some LISP variants):
fire up with an initial workspace
modify by your liking
store the result
This is unfortunately not the case. While you can import existing files, the other option is to specify an existing file as initially to be loaded and keep the interpreter open by using the -i option.
It really depends on your terminal for the exact commands.
The general idea is to copy everything (if possible) or one page at a time from the terminal into a text editor and then clean the >>> prompts (and possibly other formatting problems) in the text editor.
But anyway, typing a lot of commands directly in the execution environment if really bad practice. At least you test a handful of lines and immediately save them in a file. IDLE is great at this game...

Python 3.5.1 in Anaconda 3 from JetBrains PyCharm IDE - environment objects

I am coming into Python from R, and installed Python 3.5 with Anaconda. Now, PyCharm console has a prompt identical to an iPython Notebook, i.e. instead of >>>, it shows [1] at the command line.
After writing a toy line of code (below) in a .py document, and running it from within PyCharm, showing no errors, I was under the assumption that the function toss(), which was defined in the .py document would be ready to use in the console. However this did not seem to be the case. I ended up copying and pasting the pertinent lines of code on the console, entering, and then, finally, the function toss() was accessible to produce random examples of the roll of a die.
Logically, there has to be a smoother way of moving code from a .py file in the Editor to the environment accessible from the Python Console. But this shorter way doesn't seem to be simply running the .py file.
Code:
import random
def toss():
return(random.randint(1,6))
So how do you make the code in a Python file in the Editor accessible in the local environment?
You need to import it first. Let's say that your function toss() is in a file called foo.py then that means that you can do
from foo import toss
toss()
in your Python Console to use your function. A Python source file is, by definition, a module and you'll need to import it in order to use any functions defined there.

IDLE 3.4.3 crashes when running module

I've recently started to use Python, and I don't know much about programming.
I've installed Python 3.4.3 in my Mac OS X Yosemite, and it worked fine for a few months, but now every time I want to run a module it stops responding.
so I use IDLE and when I open a new file to define a function I can write, then I click run and I save my file, and then when I click save IDLE doesn't restart as it use to and I can't write on it or do anything else. there is no crash report, no nothing, just all frozen. I close it and if then I re open and write on IDLE it works but as soon as I try to run a new file again it freezes.
How can I solve it?
to try stuff out i've been using repl.it online and it is useful for a learner like me but still I would like to be able to use IDLE on my mac.
THANK YOU FOR YOUR HELP!!!!
Try using Anaconda, which is another Python distribution, but more science-oriented. It has all the commonly required modules already in it (plus other for scientific purposes), so that you don't have to install them as and when required. And when you install Anaconda, it automatically installs Spyder, which is a much better IDE than IDLE.

Problems installing IDLE2HTML

I'm trying to install IDLE2HTML from the following link in order to print from IDLE in color. I use Python 2.7 on a Mac.
I follow the instructions from the readme.txt, but when I reload IDLE nothing has changed. I don't have the Save As HTML option.
In the past I've installed and reinstalled Python a few times.
When trying to find my idlelib folder (as written in the readme instructions), I eventually found it under the path /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
Is my problem that my python is in /System/Library/?
Any ideas please?
I have not tried this extension, but the following may help. When you start Idle, select File / Path browser on the menu. You should see
_ sys.path
|
+ some_path/pythonxy/Lib/idlelib
|
+ some_path/python27.zip # or MAC equivalent
The first path tells you where the new files should be. If you click the +, you will see what file are there. What about IDLE2HTML.py? The next issue is whether you properly edited config-extensions.def. If you wait a few days for 2.7.9 and install it, you can try the new Options / Config-extensions dialog on the menu. That will show whether Idle can read the new information in the .def file. Or you can edit your question, paste in the new material you added, and comment on this answer, and I will take a look. However, given that the extension has been around for years, I presume that the addition in config-extensions.txt is correct.
I suspect that whenever you upgrade Python, config-extension.def will be overwritten and you will have to re-edit it.
If the problem is not a configuration problem with the IDLE2HTML Python IDLE extension like this answer (for the same question) explains about, it may be a problem with the code itself (this has happened with Python 3 IDLE). This can be checked by running IDLE using the Python file that starts it (<PYTHON LIBRARY DIR>\idlelib\idle.py where <PYTHON LIBRARY DIR> is the location of the standard library for the Python installation), in a command line interpreter (use <PYTHON EXECUTABLE DIR>\python.exe where <PYTHON EXECUTABLE DIR is the location of the executable files that start Python). It will launch IDLE and show any errors in the C.L.I..
Quote from Python idlelib module docstring:
Idle includes an interactive shell and editor. Use the files named idle.* to start Idle.
IDLE2HTML real error example:
A common problem is that IDLE2HTML version 2.0 (latest at time of writing) needs a simple code tweak to work with Python 3.x (a fixed version that works as part of the IDLEX Python module had this tweak). File comparison image (left is original file, right is IDLEX version in image); if the Python version is Python 3.x, import Tkinter needs to be changed to import tkinter as Tkinter and import tkFileDialog needs to be changed to import tkinter.filedialog as tkFileDialog:
Edwards suggestion of using the CLI for diagnosis was very useful. It exposed that the cgi.escape attribute does not exist for Python 3.
I changed the IDLE2HTML.py line 74 from "out.append(cgi.escape(content))" to
"out.append(content)" and after a re-boot it all worked!
Later: well almost... better to replace "import cgi" with "import html and then cgi.escape() with html.escape()
If using Win10 you will need super-admin rights to change IDLE2HTML.

Categories

Resources