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

Access over 20 APIs and mobile SDKs, up to 250k transactions free with no credit card required

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

#1 for location developers in quality, price and choice, switch to HERE.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}