import os
os.exit(0)
Instead of importing the whole module, is there any way to import the specific module in OS? (This could make my program more efficient when used.)
from os import _exit
This code should Import it specifically which should improve 'performance'.
Related
Let's say I have a file where I'm importing some packages:
# myfile.py
import os
import re
import pathlib
def func(x, y):
print(x, y)
If I go into another file and enter
from myfile import *
Not only does it import func, but it also imports os, re, and pathlib,
but I DO NOT want those modules to be imported when I do import *.
Why is it importing the other packages I'm importing and how do you avoid this?
The reason
Because import imports every name in the namespace. If something has a name inside the module, then it's valid to be exported.
How to avoid
First of all, you should almost never be using import *. It's almost always clearer code to either import the specific methods/variables you're trying to use (from module import func), or to import the whole module and access methods/variables via dot notation (import module; ...; module.func()).
That said, if you must use import * from module, there are a few ways to prevent certain names from being exported from module:
Names starting with _ will not be imported by import * from .... They can still be imported directly (i.e. from module import _name), but not automatically. This means you can rename your imports so that they don't get exported, e.g. import os as _os. However, this also means that your entire code in that module has to refer to the _os instead of os, so you may have to modify lots of code.
If a module contains the name __all__: List[str], then import * will export only the names contained in that list. In your example, add the line __all__ = ['func'] to your myfile.py, and then import * will only import func. See also this answer.
from myfile import func
Here is the fix :)
When you import *, you import everything from. Which includes what yu imported in the file your source.
It has actually been discussed on Medium, but for simplification, I will answer it myself.
from <module/package> import * is a way to import all the names we can get in that specific module/package. Usually, everyone doesn't actually use import * for this reason, and rather sticked with import <module>.
Python's import essentially just runs the file you point it to import (it's not quite that but close enough). So if you import a module it will also import all the things the module imports. If you want to import only specific functions within the module, try:
from myfile import func
...which would import only myfile.func() instead of the other things as well.
The error says that the there is no module named as "gtts.gTTS", and got this error many times while importing other modules. So can you brief me what is the logic behind importing modules? Can't we import class using "." operator?
What is the problem; I can't understand!
#what is the difference between these two codes
import gtts.gTTS
from gtts import gTTS
Unlike Java, where you do something like import module.submodule.blah.blah.MyClass, in Python, you can only directly import modules. If you want to only import a certain class, function, or other named value from a module, you need to use the from ... import ... syntax.
In all likelihood, gtts is a module, and gTTS is a class within that module. Therefore, import gtts.gTTS makes no sense, since gTTS isn't a module (that's what the error says), you must use from gtts import gTTS
For example, import os.path works fine, since path is a submodule of os, but if I wanted to use the exists function in path, I would need to use from os.path import exists or import os.path; os.path.exists(...). I get a ModuleNotFoundError if I erroneously try import os.path.exsts.
Im currently using:
import os
Should I be importing everything separately like this:
from os import listdir, chdir, path, getcwd
I'm looking to get my compiled .exe as small in size and optimized as possible. Is it worth doing this or does python not include unused functions and classes when compiling?
Im using pyinstaller
The import os method is more efficient in terms of execution time.
If we import the entire module:
import os
def list():
print(os.listdir('.'))
it executes in 0.074s, but when importing one method only:
from os import listdir
def list():
print(listdir('.'))
then it takes 0.076s.
Here I used the timeit module to time the execution of the above functions.
I have set of inbuilt functions in 'pythonfile1.py' located at '/Users/testuser/Documents', the file contains
import os
import sys
import time
Now i want to import 'pythonfile1.py' to 'pythonfile2.py', which is located at '/Users/testuser/Documents/execute'
I have tried with my following code and it didn't work:
import sys
sys.path[0:0] = '/Users/testuser/Documents'
import pythonfile1.py
print os.getcwd()
I want it to print the current working directory
Your question is a bit unclear. Basically, there are two things 'wrong'.
First, your import statement is broken:
import pythonfile1.py
This specifies a file name, not a module name - modules don't contain dots and extensions. This is important because dots indicate sub-modules of packages. Your statement is trying to import module py from package pythonfile1. Change it to
import pythonfile1
Second, there's no need to fetch builtins from another module. You can just import them again.
# pythonfile1
import os
print 'pythonfile1', os.getcwd() # assuming py2 syntax
# pythonfile2
import os
print 'pythonfile2', os.getcwd()
If you really want to use os from pythonfile1, you can do so:
# pythonfile2
import os
import pythonfile1
print 'pythonfile2', os.getcwd()
print 'pythonfile1->2', pythonfile1.os.getcwd()
Note that os and pythonfile1.os in pythonfile2 are the exact same module.
if you want to import stuff from another file, you should use python modules.
If you will create file named init.py then the execute folder becomes a module.
After that you can use
from .pythonfile1 import function_name
or you can use
from .pythonfile1 import *
which imports all, but better solution is name everything you want to use explicitly
you can find more about modules in documentation
Why do we need to import module1.module2 if we can just import module1?
Example:
Why do we need import tkinter.messagebox and do tkinter.messagebox.askyesno(“blah text”) when we also can do import os and still can do os.path.join(“/“, “blah”)?
I use import os in my code regularly, and I saw in someone else’s code the import tkinter.messagebox.
You can only use module1.module2 without an explicit import if module1 itself imports module2. For instance, os internally imports one of several other path-handling modules (depending on the OS) and calls it path. This path is then just a variable inside the os module that lets you access the os.path module.
This is one rationale. Generally when people do a
import os
more often than not, they use methods that belong to both os.path and os like os.path. abspath() and os.getcwd(). So importing os makes more sense. If you are assured you are going to use just the methods in os.path you might as well import os.path and it is perfectly fine.
Similarly, if you are sure you are gonna be using just the methods in tkinter.messagebox you do
import tkinter.messagebox