0

I am in the habit of using raw_input(...) for certain debugging. However, in python3 this has changed to input(...). Is there a way to define an alias at the top of my project, such as:

# __init__.py
raw_input = input

I tried the above, but it only worked in the file I added it to, and not any other files in that directory. I'd like this to work basically in every file within my python repository.

David542
  • 104,438
  • 178
  • 489
  • 842
  • Do you mean, `raw_input = input`? – xrisk Oct 04 '18 at 17:38
  • @Rishav yes, sorry. Thanks for pointing that out -- updated above. – David542 Oct 04 '18 at 17:39
  • Python 3's `input` method is already the equivalent of Python 2's `raw_input`. It's Python 2's `input` that doesn't exist anymore. – mypetlion Oct 04 '18 at 17:43
  • 1
    Nope. There's [`PYTHONSTARTUP`](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP), but that only works for interactive interpreter sessions. See also: [How to add builtin functions](https://stackoverflow.com/questions/6965090/how-to-add-builtin-functions) (tldr: not really possible) – Patrick Haugh Oct 04 '18 at 17:46
  • @PatrickHaugh got it -- so maybe I should just add that line at the top of each file I need to use it in? – David542 Oct 04 '18 at 17:47
  • It's definitely possible with something like the [site module](https://docs.python.org/3/library/site.html). Though this is likely to cause (possibly a lot) more frustration down the road. – jedwards Oct 04 '18 at 17:54

3 Answers3

3

You can define all aliases in a separate file (e.g. aliases.py) then import said file where needed (i.e. import aliases).

The con with this method that you'll be referencing the alias through aliases.alias unless you make the import stricter (i.e. from aliases import raw_input) or if you don't care about avoiding a wildcard import (i.e. from aliases import *).

Additionally, if you don't mind another import in the aliases file you can use the builtins namespace:

import builtins

builtins.raw_input = input

You still have to define all aliases separate file (e.g. aliases.py) then import said file where needed (i.e. import aliases) but the advantage of using the builtins namespace is that you can use that import exactly as given.

Fake Code Monkey Rashid
  • 13,731
  • 6
  • 37
  • 41
  • How is this different from just `raw_input = input`? – xrisk Oct 04 '18 at 17:52
  • 1
    @Rishav This makes all the aliases available to all Python files easily with one import. Instead of having to redefine them in every file. – mypetlion Oct 04 '18 at 17:54
  • 1
    It just skirts around the real problem IMO; which is how to include a certain piece of functionality without explicitly `import`ing it or adding it to the top of the file. – xrisk Oct 04 '18 at 17:58
  • Does it? `import aliases` will only pull `aliases` into the current namespace, so now OP can write `aliases.raw_input(...)`. – jedwards Oct 04 '18 at 18:01
  • @Rishav A solution that looks like what OP seems to expect it to look like would involve modifying the Python parser. Which is almost universally considered a bad idea. You're basically forking the language at that point and creating your own personal dialect. Instead of doing that, you can put one import statement in each of your Python files. – mypetlion Oct 04 '18 at 18:05
  • @Rishav: The question wasn't about how to define an alias but how to make said alias available across multiple files. There are arguably better methods. – Fake Code Monkey Rashid Oct 04 '18 at 18:06
  • @jedwards: I blanked when I thought I got the import wrong but you are correct. – Fake Code Monkey Rashid Oct 04 '18 at 18:06
  • @FakeCodeMonkeyRashid could you not do a `from aliases import *` for this purpose then? – r.ook Oct 04 '18 at 18:15
  • @Idlehands: Yes, that was my 2nd edit but I reverted it when I realized it didn't scale. My recent edit should (hopefully) cover all the bases of this method. – Fake Code Monkey Rashid Oct 04 '18 at 18:17
0

You can do it by creating a module for creating the renaming function and then importing it to every file you want to like this:

First the module function declaration in alias.py

def raw_input(a):
    return input(a)

Secondly, import to another file:

from alias import raw_input
x = raw_input("hello world")
print(x)

Sadly, you will have to make the import of the module to every file you want to use the renamed function.

Hope it works for you!

Pablo Alvarez
  • 106
  • 1
  • 7
  • 2
    How is this different from just `raw_input = input`? – xrisk Oct 04 '18 at 17:52
  • sorry, forgot to clarify that the first part of the code is within another file called alias.py (already edited), the only difference I see is that he wanted to have the renamed function in every file he uses so it is available through the import. Fake Code Monkey Rashid answered the same way of solving it just a few seconds before me. – Pablo Alvarez Oct 04 '18 at 18:10
  • 1
    @Rishav is not wrong. You don't have to define `raw_input` as a new function since `raw_input = input` essentially makes them identical objects, and you can still `import` the same from aliases. – r.ook Oct 04 '18 at 18:21
0

Put this at the top, and you will get exactly what you want.

import builtins
builtins.raw_input = builtins.input

It is guaranteed to work, but generally considered a bad practice (everybody will be confused with where is that raw_input defined)

Andrii Maletskyi
  • 2,152
  • 1
  • 15
  • 24
  • This method does define an alias but it doesn't make said alias available across multiple files. I hope you don't mind but I elaborated on this method in my answer. – Fake Code Monkey Rashid Oct 04 '18 at 19:26
  • @FakeCodeMonkeyRashid this method needs to be executed once per python process. Then `raw_input` becomes available in every module of this process – Andrii Maletskyi Oct 04 '18 at 19:41
  • I tested it and it does work as you described if the project is structured accordingly. If you are just working with a few files in an unstructured project then it is more likely to work as I described. – Fake Code Monkey Rashid Oct 04 '18 at 23:06