I'm using tkinter package for GUI programming.
Since I don't know what modules are in need during coding, I usually import all. (by typing from tkinter import *)
But I found this could cause a problem when I turned a .py file into .exe file by using pyinstaller. The result file size was too large.
To reduce the file size, I'm trying to edit the .py file by excluding unnecessary imported modules. There are lots of unused imports confirmed by pylint(it just warned me when I run .py file in terminal on Visual Studio Code). But how should I type the code to exclude these imports?
While importing from other modules its always better to import only the classes and functions that you need or else your program could face problems like taking a lot of time to run the program and takes up a lot of storage space etc.
To only import the classes and functions you need you can use
from module_name import function_name, class_name
Or else you can import tkinter itself and use the module name to access the classes and functions in it like this
import tkinter
window = tkinter.Tk()
To know more about modules you can check the documentation Modules - Python 3.9.0. And for tkinter tutorials you can checkout Tkinter Tutorials
Related
My role at work includes automating several processes which are easily automated via Python, for use by the entire team at the office. None of my coworkers have Python set up on their devices (we're all Windows) nor would they be comfortable using it if they did, so I've been compiling my work into .exe files with tkinter.
My most recent (and, of course, the most important) program compiles into a onefile .exe without issue, and runs perfectly on my computer. My coworkers are able to open it and load files into it, but when they go to activate some of the tkinter buttons within, one coworker has absolutely nothing happen while another gets a windows error message that the program has crashed. I'm unable to recreate these issues on my side.
The .exe was made using pyinstaller in a virtual environment, in which I installed all necessary dependencies. The script does not need to read or reference any other files in order to run. Is it possible that the .exe is still somehow missing a dependency that it's finding on my device and not others? How can I be sure? Would setting it up to include an installer wizard make a difference? Thanks for the help on this!
I can't include the full script due to character limit, but I've included the opening lines with all imports.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.fft import fft as fft
from scipy.fft import ifft as ifft
from tkinter import *
from tkinter import ttk
from tkinter import simpledialog
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
I have done some research and learned that python's import statement only imports something once, and when used again, it just checks if it was already imported. I'm working on a bigger project and noticed that the same thing is imported in multiple files, which aparently doesn't affect performance but leaves the code a bit polluted imo. My question is: is there a way to import something only once and use it everywhere in the directory without calling the import statement over and over?
Here are some of the modules that I'm importing in various files:
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import *
Each module (.py file) that needs to have those imported names in scope, will have to have its own import statements. This is the standard convention in Python. However, it's not recommended to import * but rather to import only the names you will actually use from the package.
It is possible to put your package import statements in a __init__.py file in the directory instead of in each .py file, but then you will still need a relative import statement to import those names from your package, as described here: Importing external package once in my module without it being added to the namespace
I am writing an application in C# with VisualStudio and am using IronPython to write some Python scripts for my application. However, it does not have the entire standard library support by default. So to import some modules (such as os) I need to point my C# code to where the os module actually is. I also understand that it will still be limited to libraries implemented in pure python.
Ultimately I want to have something that can be installed on another machine. My current workaround is to include a copy of https://github.com/python/cpython/tree/2.7/Lib in the Debug folder where the executable is running and it seems excessive/unnecessary to have to include the entire thing. I tried just placing the files I need (for example os.py) here but obviously it imports other modules, which import other modules, etc... I would have to re-run the code to get the error for which module it couldn't find and add them in 1 by 1 and it was getting too tedious.
I was wondering if there was any sort of resource that specifies the relationships between standard library modules and could tell me exactly what files to copy. Essentially what I'm looking for is the graph of the standard library imports. So if I want to import os in these scripts I know to copy os.py, ntpath.py, ...
Thanks
you probably don't need the imports as a tree, but as a simple list, so you can just copy the needed files. You can get that from sys.modules, after you import everything that your script needs - it will contain all modules needed by those that you imported, e.g.:
import sys # even if you don't use it - it's a built-in module, won't add a file to the list, needed to get sys.modules
import os
import time
#import whatever-else
# this gives a list of tuples (module,file)
m=[(z,x.__file__) for z,x in sys.modules.items() if hasattr(x,"__file__") ]
for x in m:
print x[0],x[1]
I know this is a long shot but I'm hoping someone on here has any experience with this as I can't find anything mentioning it online. I have a python script that imports modules from Autodesk Maya. This python script is run through mayapy.exe instead of python.exe which is whats throwing me off. I would rather not have to include a bat file with my script and have the users set the location of mayapy.exe in order to use it. I would prefer to somehow package mayapy.exe with my script using something like py2exe. I'm a little lost on where to go from here honestly.
If I run py2exe normally on my script, its result gives me the error cannot find maya.cmds, as expected. Is there a way I can find the dll to include? I tried running Dependency walker on mayapy.exe but I'm venturing into new territory here. There were only 2 dll's used from the Maya install directory base.dll and python26.dll, the rest were all system dlls. If anyone has tried any of this please share or if anyone has any advice or path I can look down or a website I can go to I would be very grateful. Thanks so much!
P.s. In case it helps at all, this is the python script imports:
try:
import maya.standalone
maya.standalone.initialize()
except:
pass
import maya.cmds as cmds
import maya.mel as mel
from time import time as tTime
from glob import iglob
from shutil import copy
from os.path import join
from PyQt4 import QtCore
from PyQt4 import QtGui
The point of the script is to create a maya file, do some things in it, then save it. Using the maya interpreter(mayapy.exe) it does this without ever opening maya, which is what I was wanting.
Pretty sure what I wanted to do was not possible. Instead I split my program into two parts, one requiring Maya one not, for the part requiring Maya I just had it do a registry lookup for the Maya install location and used that. If it were possibly to include the necessary files for my script, I'm sure Autodesk would not have approved in the first place anyways.
First of all let me tell you that I'm a new user and I'm just starting to learn Python in College so my apologies if this question is answered in other topic, but I searched and I can't seem to find it.
I received a file work.pyc from my teacher and he says I have to import it in my Wing IDE using the command from work import *, the question is I don't know where to put the file to import it.
It just says ImportError: No module named work.
Thank you
There are several options for this.
The most straightforward is to just place it in the same folder as the py file that wants to import it.
You may also want to have a look at this
if you're using the python interpreter (the one that lets you directly input python code into it and executes) you'll have to do this:
sys.path.append('newpath')
from work import *
where newpath is the path on your filesystem containing your work.pyc file
If you're working on a script called main.py in the folder project, one option is to place it at project/work.pyc
This will make the module importable because it's in the same working directory as your code.
The way Python resolves import statements works like this (simplified):
The Python interpreter you're using (/usr/bin/python2.6 for example, there can be several on your system) has a list of search paths where it looks for importable code. This list is in sys.path and you can look at it by firing up your interpreter and printing it out like this:
>>> import sys
>>> from pprint import pprint
>>> pprint(sys.path)
sys.path usually contains the path to modules from the standard library, additional installed packages (usually in site-packages) and possibly other 3rd party modules.
When you do something like import foo, Python will first look if there is a module called foo.py in the directory your script lives. If not, it will search sys.path and try to import it from there.
As I said, this explanation is a bit simplified. The details are explained in the section about the module search path.
Note 1:
The *.pyc you got handed is compiled Python bytecode. That means it's contents are binary, it contains instructions to be executed by a Python virtual machine as opposed to source code in *.py that you will normally deal with.
Note 2:
The advice your teacher gave you to do from work import * is rather bad advice. It might be ok to do this for testing purposes in the interactive interpreter, but your should never do that in actual code. Instead you should do something like from work import chop, hack
Main reasons:
Namespace pollution. You're likely to import things you don't need but still pollute your global namespace.
Readability. If you ever read someone elses code and wonder where foo came from, just scroll up and look at the imports, and you'll see exactly where it's being imported from. If that person used import *, you can't do that.