Import sys versus from sys import...?

In Ex13 (LP3THW), the first line is “from sys import argv”, so (I think), the program is asking the interpreter to import the argv function/class/whatever from the sys module/library/whatever. In Ex23, the line is just “import sys”. Does this import the entire sys module, with all included functions/classes/whatevers? In Ex35, it’s back to importing just one function: “from sys import exit”. Now, in looking things up online, I’ve found code examples that include similar lines, as well as “from sys import *”. I’m pretty sure, though I can’t find it now, I’ve also seen “from sys import *” on line 1, and "from sys import " on line 2, or something similar.

So, when should you import a single function, when should you import the whole library, and when should you - apparently? - do both? Is “from sys import " the same as “import sys”, or is "” actually an individual function, similar to “argv”? (I’ve been assuming it’s just a wildcard.)

1 Like

In a nutshell:

  • import fruit makes the module fruit available in the current scope. Anything in that module can be accessed with dot access, e.g.: print(fruit.banana). Do this.

  • from fruit import banana makes the name banana (which is defined in fruit) directly available in the current scope: print(banana) Do this is if you need very few specific things from a module.

  • Never use the form from fruit import * except for quick and dirty hacks. This will blanket import everything from fruit and is too likely to cause name clashes.

You can mix both forms freely, and import statements can theoretically be anywhere in a file, even inside functions, but they should be at the top.

1 Like

What @florian said…

I struggled with this when I started. I couldn’t understand the module and class part and it baffled me when I saw things like:

from app import App

Once I got that, I then got confused as to when to use the different imports styles too.

These days I consider the size of the module I’m importing and how much I’m using. In some cases there’s best practice like:

import pandas as pd
1 Like

So, what does “import pandas as pd” do?

It imports a module under another (usually shorter) name.

import pandas as pd
pd.do_great_stuff()
1 Like

Yeah, the ‘pd’ alias is just a convention. You could call it ‘banana’ or ‘excellent_data_module’ but that lacks the brevity of ‘pd’.

It’s a useful trick if the module title you are using is lengthy, like ‘selenium_webdriver’ because you can alias to something you like.

1 Like
import numpy as the_standard_library_for_numerical_computations

To enhance code readability… :stuck_out_tongue_winking_eye:

2 Likes

Can anyone comment if there are memory or performance benefits for choosing one method over the other?

My gut feeling is it would be better to only import the bare minimum you need, but perhaps the compiler strips out anything not used regardless, so maybe it doesn’t matter…?

Exactly that. If your imported module is massive then only importing what you need is faster. How much faster on modern computers is questionable.

Actually this might be a fun test for performance. I might create a module with 1000 functions (that print function name) and comparing calling the entire module with just one function. I still think it will be negligible difference.

1 Like

I daresay when performance is so important that you actually need to care about this, then you won’t be using Python anyway.

2 Likes

Yes totally agree. A more tangible Python performance improvement would be using comprehensions over nested loops.

A free service run by Zed A. Shaw for learncodethehardway.org.