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

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

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

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}