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.
Related
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
generic_import.py
def var_import():
import sys
import glob
main.py
from generic_import import *
var_import()
whenever I run the main file, I get
NameError: name 'sys' is not defined
However when import sys is outside the function,the code is executed without any error.
generic_import.py
import sys
def var_import():
import glob
What's the reason behind this ? I want to import it inside the fucnction.
The reason for this is the Scope of the Imports.
You are importing the libraries inside a function so the scope of these is var_import and as soon as the function terminates the scope is discarded. You would need to return the imported libraries and then save them inside the scope you want to use them.
But I would recommend just import libraries as needed without any generic_import functionality.
If you are worried about namespace conflicts: You can always use aliases like import sys as python_builtin_sys but I wouldn't recommend this either.
Since you asked about how to get the modules outside the function scope I'll provide a short example code.
def var_import():
import sys
import glob
return sys, glob
and you can get them into your wanted scope by using something along the lines of:
from generic_import import var_import # Do not use "import *"
sys, glob = var_import()
or something more advanced if you don't know the number of loaded modules.
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'.
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
I have two files, SysDump.py and libApi.py in the same folder.
In SysDump I do:
from libApi._SysDump import *
In libApi I have:
def _SysDump():
import cPickle as _cPickle
import math as _math
from zipfile import ZipFile as _ZipFile
import re as _re
However I get the error:
from libApi._SysDump import *
ImportError: No module named _SysDump
I use VS2012+PTVS to step through the code and the execution trace goes to def _SysDump() in libApi as I steop through but does not enter it. Question is how do I make this work in Python 2.6 only please?
from libApi._SysDump import *
When writing this, Python looks for a package libApi and a module in it called _SysDump. A package is equivalent to a folder and a module is a single file. From your explanations, this is not the situation you have in your case. You have a module libApi with a function _SysDump. So if anything, you could do this:
from libApi import _SysDump
So you would get a reference to the _SysDump function. Note that running that function will not give you references to all the modules you are trying to import. Inside the function, the modules will be imported and assigned to local variables. After the function ends, those references are gone.
If you want to have some module take care of all your imports, you could make a file that performs those imports and import everything from that module:
# imports.py
import cPickle as _cPickle
import math as _math
from zipfile import ZipFile as _ZipFile
import re as _re
And then:
from imports import *