Over a million developers have joined DZone.

Just me? A Quick Python / Bash Tip

DZone's Guide to

Just me? A Quick Python / Bash Tip

· Web Dev Zone ·
Free Resource

Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.

Does this happen to anybody else besides me?

$ imåport datetime
-bash: import: command not found

I’d say at least once a day I fire up a new shell and start typing Python code without remembering to launch the interpreter first. Luckily, that Python code usually starts with an import statement. Here’s a quick hack I just put in my .bashrc to fix the problem:

fake_import() {
    python -ic "import $1"
alias import=fake_import

Now I can just type import datetime and a new Python interpreter will launch with the datetime module already imported:

$ import datetime
>>> datetime.datetime.utcnow()
datetime.datetime(2011, 12, 2, 16, 15, 29, 614887)

The only weird thing is that I have to define the function with a different name and then alias it to import. I guess import is a Bash keyword? I’m not a Bash expert, so does anybody have any better ways to do this?

Source: http://blog.fiesta.cc/post/13779830400/just-me-a-quick-python-bash-tip

Get the senior executive’s handbook of important trends, tips, and strategies to compete and win in the digital economy.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}