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

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}