Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading