tags:

views:

320

answers:

6

There is a snippet of code that I would like to copy and paste into my Python interpreter. Unfortunately due to Python's sensitivity to whitespace it is not straightforward to copy and paste it a way that makes sense. (I think the whitespace gets mangled) Is there a better way? Maybe I can load the snippet from a file.

This is just an small example but if there is a lot of code I would like to avoid typing everything from the definition of the function or copy and pasting line by line.

class bcolors: 
    HEADER = '\033[95m' 
    OKBLUE = '\033[94m' 
    OKGREEN = '\033[92m' 
    WARNING = '\033[93m' 
    FAIL = '\033[91m' 
    ENDC = '\033[0m' 

    def disable(self):  
        self.HEADER = '' # I think stuff gets mangled because of the extra level of indentation 
        self.OKBLUE = '' 
        self.OKGREEN = '' 
        self.WARNING = '' 
        self.FAIL = '' 
        self.ENDC = ''
+4  A: 

You can just import the file into the python interpreter. This will load the class in, and allow you to run the code.

For instance, create a file named "bgcolors.py" and copy and paste your code inside. Then using the python interpreter, you just type "import bgcolors" and you should be able to run it.

You can read more here:

http://docs.python.org/tutorial/modules.html

Anton
A second `import bgcolors` in the same session will do nothing because import "knows" bgcolors has already been imported.
msw
Rather than importing it explicitly in the shell, call python like so: `python -m bgcolors.py` and you will be presented with an environment where that module is already loaded.
vezult
I think the -m option only works for file in sys.path. But my script is not in the standard library.
wp123
@wpeters: It works just like any import. If your module is not in the current directory, in any bourne compatible shell, you can invoke python like so: `PYTHONPATH=/path/to/module/dir python -m bgcolors.py` , or by any other means, set the PYTHONPATH environment variable.
vezult
+4  A: 

You can use IPython which is much better python repl. It has command for getting input from external editor by using %edit command.

Łukasz
I like this solution because it doesn't require you to use a different IDE.
wp123
+2  A: 

The IDLE interface does go to effort to preserve the proper indentation of pasted text.

msw
+1  A: 

Dreampie allows you to copy and paste code with proper indentation.

Anarchist
+2  A: 

You can call execfile(filename). More or less the same as importing a module, except that it skips the module administration part and doesn't require you to add a folder to sys.path.

EDIT: To address the original question: copy-pasted code can be executed by calling exec(codestring).

Pieter Witvoet
how does this answer the question "how do I paste python code?"
Bryan Oakley
You are correct that it does not really, but I prefer this solution because it doesn't require you to pick a specific shell or use a specific IDE. The other solutions are also excellent but I accepted the one that is most general.
wp123
@Bryan Oakley: You're right, execfile(filename) can't be used for that. exec(codestring) can, though.
Pieter Witvoet
+2  A: 

You can usually easily and safely do copy-pasting with IPython, through the commands %cpaste and %paste. This is very handy for testing code that you copy from web pages, for instance, or from your editor. IPython also has a %run command that runs a program and leaves you in a Python shell with all the variables that were defined in the program, so that you can play with them.

EOL