pymapdl
pymapdl copied to clipboard
Black MAPDL window opens when compiling a program with PyMAPDL
๐ค Before submitting the issue
- [X] I have visited the Troubleshooting section.
- [X] I have searched among the existing issues.
- [X] I am using a Python virtual environment.
- [X] I am using the latest version of PyMAPDL and its dependencies (fully updated virtual environment). You can update them using
pip install --upgrade --upgrade-strategy eager ansys-mapdl-core
in your activated virtual environment.
๐ Description of the bug
When using pymapdl and starting a MAPDL instance using the function launch_mapdl
, MAPDL runs in the background and does not display any window which is the expected behavior. However, when packaging a script that uses this function, a black MAPDL empty console will appear and remain on screen as long as the instance is running.
This is a problem for any program with a GUI using pymapdl that needs to be compiled. I am not able to test on Linux to see if the problem also exists but it could be simply solved on Windows by adding the following argument creationflags=0x08000000
in the subprocess command that calls the MAPDL executable file.
๐ต๏ธ Steps To Reproduce
# Package the following code with PyInstaller using the noconsole argument.
from time import sleep
from ansys.mapdl.core import launch_mapdl
mapdl = launch_mapdl()
print(mapdl)
sleep(10)
mapdl.exit()
๐ป Which Operating System are you using?
Windows
๐ Which Python version are you using?
3.11
๐พ Which MAPDL version are you using?
23.1
๐ PyMAPDL Report
Show the Report!
PyMAPDL Software and Environment Report
Packages Requirements
*********************
Core packages
-------------
ansys.mapdl.core : 0.68.0
numpy : 1.26.4
platformdirs : 4.2.0
scipy : 1.12.0
grpc : Package not found
ansys.api.mapdl.v0 : Package not found
ansys.mapdl.reader : 0.53.0
google.protobuf : Package not found
Optional packages
-----------------
matplotlib : 3.8.3
pyvista : 0.43.3
pyiges : 0.3.1
tqdm : 4.66.2
Ansys Installation
******************
Version Location
------------------
222 C:\UserProg\ANSYS Inc\v222
231 C:\UserProg\ANSYS Inc\v231
Ansys Environment Variables
***************************
CADOE_LIBDIR231 C:\UserProg\ANSYS Inc\v231\CommonFiles\Language\en-us
AWP_ROOT222 C:\UserProg\ANSYS Inc\v222
ANSYS231_DIR C:\UserProg\ANSYS Inc\v231\ANSYS
AWP_LOCALE222 en-us
ANSYS222_DIR C:\UserProg\ANSYS Inc\v222\ANSYS
AWP_ROOT231 C:\UserProg\ANSYS Inc\v231
CADOE_LIBDIR222 C:\UserProg\ANSYS Inc\v222\CommonFiles\Language\en-us
AWP_LOCALE231 en-us
๐ Installed packages
Show the installed packages!
alabaster==0.7.16
altgraph==0.17.4
ansys-api-mapdl==0.5.1
ansys-api-platform-instancemanagement==1.0.0
ansys-mapdl-core==0.68.0
ansys-mapdl-reader==0.53.0
ansys-math-core==0.1.3
ansys-platform-instancemanagement==1.1.2
ansys-tools-path==0.4.1
appdirs==1.4.4
Babel==2.14.0
certifi==2024.2.2
chardet==5.2.0
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
commonmark==0.9.1
contourpy==1.2.0
coverage==7.4.4
cx-Logging==3.1.0
cx_Freeze==6.15.15
cycler==0.12.1
docutils==0.20.1
et-xmlfile==1.1.0
fonttools==4.49.0
geomdl==5.3.1
grpcio==1.62.0
idna==3.6
imagesize==1.4.1
importlib-metadata==7.0.1
iniconfig==2.0.0
Jinja2==3.1.3
kiwisolver==1.4.5
lief==0.14.0
MarkupSafe==2.1.5
matplotlib==3.8.3
numpy==1.26.4
openpyxl==3.1.2
packaging==23.2
pdfkit==1.0.0
pefile==2023.2.7
pillow==10.2.0
platformdirs==4.2.0
pluggy==1.4.0
pooch==1.8.1
protobuf==3.20.3
psutil==5.9.8
pyansys-tools-versioning==0.5.0
Pygments==2.17.2
pyiges==0.3.1
pyinstaller==6.4.0
pyinstaller-hooks-contrib==2024.1
pyparsing==3.1.1
pytest==8.1.1
pytest-cov==5.0.0
pytest-rerunfailures==14.0
python-dateutil==2.8.2
pyvista==0.43.3
pywin32-ctypes==0.2.2
PyYAML==6.0.1
recommonmark==0.7.1
reportlab==4.1.0
requests==2.31.0
rinoh-typeface-dejavuserif==0.1.3
rinoh-typeface-texgyrecursor==0.1.1
rinoh-typeface-texgyreheros==0.1.1
rinoh-typeface-texgyrepagella==0.1.1
rinohtype==0.5.4
rst2pdf==0.101
scipy==1.12.0
scooby==0.9.2
six==1.16.0
smartypants==2.0.1
snowballstemmer==2.2.0
Sphinx==7.2.6
sphinx-markdown-builder==0.6.6
sphinx-rtd-theme==2.0.0
sphinxcontrib-applehelp==1.0.8
sphinxcontrib-devhelp==1.0.6
sphinxcontrib-htmlhelp==2.0.5
sphinxcontrib-jquery==4.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.7
sphinxcontrib-serializinghtml==1.1.10
tabulate==0.9.0
tqdm==4.66.2
urllib3==2.2.1
vtk==9.3.0
zipp==3.17.0
๐ Logger output file
Show the logger output file.
# PASTE HERE THE CONTENT OF THE LOGGER OUTPUT FILE.
Hi @PaulCreusy
I haven't work much on packing PyMAPDL in an executable using pyinstaller. Can you share the code you use to create the compiled executable (aka the pyinstaller commands or files)??
Hi, thank you for your quick answer, here is a script that will allow you to compile the example included in the issue. The example should be placed in a file called main.py for this script to work.
Also because some files must be included manually after the packaging, please make sure that the versions of the pyansys_tools_versioning and ansys_tools_path are the ones specified in my pip freeze or in the paths below. Otherwise, please change them to match yours.
import os
from distutils.dir_util import copy_tree
from site import getsitepackages
# Find the location of Python's site packages directory
print(getsitepackages())
site_packages_path = getsitepackages()[-1]
PACKAGE_NAME = "MyPackage"
# Package with PyInstaller
os.system(f"pyinstaller --noconsole -n {PACKAGE_NAME} main.py")
# Include additional files to avoid crash on start
copy_tree(site_packages_path + "/ansys/api", f"dist/{PACKAGE_NAME}/_internal/ansys/api")
copy_tree(site_packages_path + "/pyansys_tools_versioning-0.5.0.dist-info",
f"dist/{PACKAGE_NAME}/_internal/pyansys_tools_versioning-0.5.0.dist-info")
copy_tree(site_packages_path + "/ansys_tools_path-0.4.1.dist-info",
f"dist/{PACKAGE_NAME}/_internal/ansys_tools_path-0.4.1.dist-info")
Probably you can replace the versions in the hardcoded paths using:
try:
import importlib.metadata as importlib_metadata
except ModuleNotFoundError: # pragma: no cover
import importlib_metadata
__version__ = importlib_metadata.version("ansys-mapdl-core")
Or maybe just importing the library and read the version:
- https://github.com/ansys/pyansys-tools-versioning/blob/1752af086157a3244b51c15f5d16cb70bf975c26/src/ansys/tools/versioning/init.py#L32C1-L32C12
- https://github.com/ansys/ansys-tools-path/blob/ac1ddc391ec55e1aa56747cefdb3f45bb264ff70/src/ansys/tools/path/init.py#L9
I am currently quite busy with other projects, I won't have time to check this until later next week. I hope this is OK. Please keep pinging here if I dont contact you back by then.
GitHub
Utilities for backwards and forward server support. - ansys/pyansys-tools-versioning
GitHub
Library to detect Ansys products installation path - ansys/ansys-tools-path
The problem is not urgent for me so later next week would be perfect. Thank you for your help and for the version tip. It will be very helpful for me!
Hello @germa89, do you have news about this issue ?
Hi @PaulCreusy
I am re-reading your issue:
When using pymapdl and starting a MAPDL instance using the function launch_mapdl, MAPDL runs in the background and does not display any window which is the expected behavior.
The default Windows OS behaviour is to open another window which can be a black MAPDL window, but it might be minimized by default. At least, that is what I see when I run it in my Windows 11 machine. There is always another window whenever MAPDL is running.
If you click on that minimized window, the window will maximize it showing a black window which is probably what you are seeing also when you compile the image.
Hence, I think when you compile the image, you cannot avoid seeing that black window, because you see it also when you are working normally (using python scripts).
@clatapie can you confirm my statements? Is there always a black windows running on Windows OS?
it could be simply solved on Windows by adding the following argument creationflags=0x08000000 in the subprocess command that calls the MAPDL executable file.
Regarding this, this is a very interesting tip. If that works, I might implement it in PyMAPDL so MAPDL is always hidden to the user. Thank you !
Reference: https://stackoverflow.com/questions/2935704/running-shell-commands-without-a-shell-window
Stack OverflowWith either subprocess.call or subprocess.Popen, executing a shell command makes a shell window quicky appear and disappear.How can I run the shell command without the shell window?
Thank you for your answer and continuing to work on this issue. I am not sure of the meaning of this sentence :
Hence, I think when you compile the image, you cannot avoid seeing that black window, because you see it also when you are working normally (using python scripts).
So just in case I would like to clarify the point. When using Python scripts I don't see the MAPDL console window. It is minimized automatically as expected and the one thing I can notice is an icon on my tasks bar. It is only for compiled images that the MAPDL window appears in foreground.
Oh.. OK you want the MAPDL window to be minimized. I see.
Well, I guess we could add subprocess.CREATE_NO_WINDOW to the subprocess call. But I will need to check side effects for this. Since I'm quite busy with other projects, I'm keeping this issue open to address this.
However, feel free to propose that change in a PR.