0

Is there any differences among the following two statements?

import os
import os as os

If so, which one is more preferred?

5
  • Do you know the difference between those two statements? Commented Jul 17, 2015 at 6:31
  • 1
    Yes, the second is longer and slightly slower because of completely redundant as os Commented Jul 17, 2015 at 6:33
  • You can check out this previous answer for some more background, but in your example there is no functional difference. stackoverflow.com/a/193931/5066845 Commented Jul 17, 2015 at 6:36
  • 2
    It's the same as the difference between x = 3 and x = 3; x = x. The second version is completely pointless; if you're wondering why the second version is something you can even write, it is because no Turing-complete programming language can exclude the possibility of writing pointless things. Commented Jul 17, 2015 at 6:36
  • All the comments about the second one longer and results in extra assignment are all wrong - dis.dis('import os') and dis.dis('import os as os') shows identical bytecode being produced - therefore they are functionally identical - there is now a valid use case - see thread. Commented May 2, 2023 at 23:37

5 Answers 5

5

It is just used for simplification,like say

import random
print random.randint(1,100)

is same as:

import random as r
print r.randint(1,100)

So you can use r instead of random everytime.

Sign up to request clarification or add additional context in comments.

Comments

1

Is there any differences among the following two statements?

No.

If so, which one is more preferred?

The first one (import os), because the second one does the exact same thing but is longer and repeats itself for no reason.

Comments

0

The below syntax will help you in understanding the usage of using "as" keyword while importing modules

 import NAMES as RENAME from MODULE searching HOW

Using this helps developer to make use of user specific name for imported modules. Example:

 import random 
 print random.randint(1,100)

Now I would like to introduce user specific module name for random module thus I can rewrite the above code as

 import random as myrand
 print myrand.randint(1,100)

Now coming to your question; Which one is preferred? The answer is your choice; There will be no performance impact on using "as" as part of importing modules.

1 Comment

import ... as ... from is invalid.
0

If you want to use name f for imported module foo, use

import foo as f
# other examples
import numpy as np
import pandas as pd

In your case, use import os

Comments

0

The import .... as syntax was designed to limit errors.

This syntax allows us to give a name of our choice to the package or module we are importing—theoretically this could lead to name clashes, but in practice the as syntax is used to avoid them.

Renaming is particularly useful when experimenting with different implementations of a module.

Example: if we had two modules ModA and ModB that had the same API we could write import ModA as MyMod in a program, and later on switch to using import MoB as MyMod.

In answering your question, there is no preferred syntax. It is all up to you to decide.

1 Comment

Don't know why you were downvoted but I upvoted. Basically I can write import sqlite3 as sql and code everything as sql.Xxxxx. Later on I can change import to import MySql as sql and not have to change hundreds or thousands of lines of code which stay as sql.Xxxxx. Good answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.