mak47
mak47

Reputation: 303

How do I import a full package but still call specific methods from a package directly?

I'm trying to both a module and an import from a module.

I've been using from time import sleep for a few years now, but I'm wanting access to more of the features from the package.

My issue is if I use import time I lose the ability call sleep() directly.

I've tried;

import time
from time import sleep

But I get the error:

    loop_time = time()
TypeError: 'module' object is not callable

and if I try importing the whole package and remove from time import sleep I get the error:

    sleep(1)
NameError: name 'sleep' is not defined

I can use the rename all function within visual studio to update sleep(x) to time.sleep(x) but I was wondering there is a different way/better way to do this? I don't have a large issue because I don't use the word sleep anywhere in my code but I expect if there was a word I couldn't rename all with this would be a much more annoying issue.

Upvotes: 0

Views: 20

Answers (1)

monk
monk

Reputation: 676

Use the * keyword.

from time import *

But beware, it is generally bad practice as you might get some naming clashes, and using time.sleep() makes it clear to whoever reading the code which module you are using.

Upvotes: 2

Related Questions