where_am_I
where_am_I

Reputation: 11

Difference between "import random" and "import sys, random" in python?

I'm making a random name generator, but my guide book uses import sys, random instead of just import random. I tried looking at Google to see what the differences between the two imports are, but I couldn't find anything. Is there any difference between the two statements?

Upvotes: 0

Views: 903

Answers (3)

Piotr
Piotr

Reputation: 712

Separating imports with commas as below

import random, sys

is the same as

import random
import sys

However, according to PEP8, it is recommended to separate your imports to ensure clarity. Moreover, in case any of the imports fail the traceback is more informative and you know exactly which library failed.

Upvotes: 4

montw
montw

Reputation: 105

import sys, random is importing 2 different modules: sys and random. However, import random will only import the random module. If the script in your book is importing the sys as well as the random module, it probably means it will also use system-related features from the sys module.

Upvotes: 0

Chris
Chris

Reputation: 1321

It means that in addition to importing the random library, they are importing the sys library as well, which means they are probably accessing some functions in that library. Check the entire code to see where they access it.

Upvotes: 3

Related Questions