How to check for python script requirements on windows - python

I have been developing a python script for a few days now, and wanted to send the final result to a friend of mine. Is there a way to get all software requirements to run my program?
The python part is relatively easy, as I can just look at my import list on my script. The problem is external software requirements.
For example, one of the python modules I'm using is Rasterio. I could not install this through conda or pip, until I installed Visual Studios C++ Community Version. I imagine there is some specific dll file that is being used. I can't really tell though.
I would really like to make a standalone setup.bat file, that installs all requirements and finally allows the user to simply run my program. I'm not 100% sure right now of those requirements though.
How can I get these?

You can explicitly check your application dependencies using Dependency Walker get it from here

Related

Embedding python and numpy in a portable C++ application with CMake

I am currently writting a C++ application. I want to use a library that is exclusively written in Python and that uses numpy. I know that there is an extensive documentation on how to embed Python in a C++ program. My problem comes from the fact that with this method, there needs to be a Python installation on the machine where the app will be installed. However, I want to be able to distribute the program on machine where the user does not have all the rights.
If I am not mistaken, Blender is doing something very similar to what I want to achieve. Basically, when you download a portable version of Blender, you will find a structure like this:
blender/
2.90/
datafiles/
python/
bin/ <-- Contains the Python executable and Python DLL
DLLs/ <-- Contains the base packages and DLLs of a standard Python installation
(such as libssl, asyncio, ...)
lib/
[...] <-- Different python packages
site-packages/ <-- Additional packages such as numpy
scripts/ <-- Contains the python scripts relative to Blender
Basically, Blender ships with its own Python installation with the correct packages. I tried to look at the Blender source code to understand how they did it, but I was quickly overwhelmed by the complexity of the code. Still, I managed to find some clues in the blender/source/creator CMakeLists.txt.
I am using CMake (3.10) for my build system and using MinGW for the compilation. I plan to support Linux sometime in the future, but I first want to concentrate on Windows. Here is a little overview on my what my objectives are:
I want that CMake automatically creates a structure like the one shown above, with the desired packages. For me it is ok if this kind of structure is created in the ${CMAKE_BINARY_DIR}. Bonus point if it is possible to automatically download locally Python and install the packages (to avoid messing with the default Python installation on the system of the developper).
I need to tell my C++ application to use the correct Python environment (the one that will be created in 1.)
P.S.
I am also considering using pybind11, which has a embedding part. But the problems are more or less the same as above.

python and package installment while application is installed

I have done some image processing works using python 3.5, opencv, scikit modules etc for an unreal engine game application.
I have manually installed python and other modules using pip in my windows system.
Now when a user installs the application, i want python and those modules to be installed auto with the application's installment.
I saw pyinstaller which turns py file to application file but unfortunately could not understand how to work it of what i want.
Thank you for any piece of advice.
First, let me say Python packaging has improved a lot over the years, but is still considered very hard compared to other languages like e.g. golang.
Generally, I see two ways how to bring your applications to your user.
Either make a Python package or create an installable package for an operation system.
A Python package means, you could upload it somewhere (e.g. PyPi) and your users could pip install your_package. This involves a lot of work. A good starting point would be:
https://packaging.python.org/tutorials/packaging-projects/
The second option is to create an installer or e.g. Windows.
There are several tools out there, like the mentioned pyinstaller, more on this page: https://docs.python-guide.org/shipping/freezing/
Also, there is a new option called PyOxidizer ( https://pyoxidizer.readthedocs.io/en/stable/overview.html ).
At work we used cx_Freeze - which worked ok.
Unfortunately, there is no easy way. Have a look at several options, and then decide for one.

What are the bare minimum files/libraries required by Python to run?

I am new to Python having come from a proprietary compiled language (Xojo) that produces self-contained executables.
I understand that Python is an interpreted language. I understand that it requires an interpreter (let’s stick with CPython) and presumably it requires a number of accessory frameworks/C libraries in order to run. What I don’t understand is why is it so hard to create a folder containing the interpreter and all required files and libraries and simply bundle these up with my script to distribute.
I have discovered that there are a bunch of tools that attempt to do this (py2app, cx_freeze, etc) but many of them seem either broken, not maintained or really buggy.
I guess my question is: is there any documentation that describes the exact things I need to bundle with a “Hello World” script to get it running? This seems to be a really straightforward problem to solve but it hasn’t been (which suggests that it is far more complex than I appreciate).
My understanding is that PyInstaller works fine for making a single exe for distribution. But barring packaging tools like that, in general, there isn't an obvious "bare minimum"; the modules don't have documented dependencies, so it's usually best to ship the whole standard library.
Typically, if you need a redistributable version, you use the embedded Python zip redistributable, shipping Python alongside your main application.
The exact list of files/libraries depends on how the python interpreter is built. In windows for example, you can obtain CPython binaries built from Visual Studio, Cygwin and Mingw-w64. They have different dependency of cause. In Linux distributions, python is normally installed by default.
Below is the list of .dll and .exe files that you can find in the official CPython release for windows.
libcrypto-1_1-x64.dll python.exe python37.dll sqlite3.dll
libssl-1_1-x64.dll pythonw.exe python3.dll vcruntime140.dll
The total size of this ZIP file release is only 6.7 MB. So it would be easy to bundle it in your main executable. You can use whatever bundler at hand, not necessary those designed for python. Quoting from the documentation here:
extracting the embedded distribution to a subdirectory of the application installation is sufficient to provide a loadable Python interpreter.
I feel the absolute best way to experience Python for beginners in thonny and an esp32.
A very good way to get started with python is to use Anaconda https://www.anaconda.com/distribution/#download-section - this distribution contains the CPython interpreter and the most commonly used packages. For quite a while you will get along without installing more packages.
To be able to make a simple distributable piece of code just include a requirements.txt along with your code which should list down the packages (and versions) you are using in your code.
More on that here : https://www.idiotinside.com/2015/05/10/python-auto-generate-requirements-txt/
pip freeze generates a superset of all packages in your running environment so you would ideally go with the second smarter option in the link : pipreqs
So, in short along with your code just an additional requirements.txt should be fine using which people can install all required packages as
pip install -r requirements.txt
and they are good to go to run your code.
For advanced scenarios you might want to look up creating virtual environments using conda.
What is a conda environment?
https://docs.conda.io/projects/conda/en/latest/user-guide/concepts.html#conda-environments
How to create/manage a conda environment
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
All the best in your Python journey!

How can I run my python scripts on a windows machine without installing extra libraries?

I'm working on a python script but it uses a library called Selenium to perform most of its tasks. This script needs to be able to run in a windows environment and I can't go through the trouble of installing Selenium on every machine that I decide to run this script on. I've heard of py2exe but haven't tried it out and am not sure if it will help here. Will I be able to run my script without having to install selenium when using py2exe? It doesn't seem like it would work since selenium isn't built into python.
I'd like to be able to run this script on Windows without having to install python but I'll be satisfied if I can get it to work without having to install selenium. I'm not sure how possible this is and I'm actually fairly new to developing in python so I don't know if this idea will work out.
Actually, other people will be using this script and it would be best if they don't have to go through the trouble of installing python and the selenium library just to use the script. They aren't exactly familiar with python or programming in general, so installing selenium could be a problem for them. I'd like to make the use of my script as user friendly as possible.
The script mainly deals with automating a website. I understand that this can be done in vbs and it would be better in vbs for windows but I've already made it in python and don't want to go through the trouble of re-writing it in vbs, since I've already spent a lot of time on this already.
I've tried many Google searches and can't find much about this topic which leads me to believe it's not possible. The closest solution I've found is py2exe but will it still work if my script uses a library not built into python?(selenium)
I'm sorry if this question was kind of nooby, I'm just trying to learn. I appreciate any help! :)
Yes, py2exe will build an executable that includes python so that python will not need to be installed on any extra computers. When you build the executable with py2exe you can include or exclude modules in your build.
PyInstaller is another good module for building executables, it will automatically bundle any includes. Like py2exe you can bundle everything into one executable, include extra data files, hide console windows and more.
You can include external libraries in py2exe
From the py2exe FAQ,
How does py2exe decide which modules you need?
To determine which modules should go in the final .exe file, py2exe does a recursive search of the script that you are packaging to find its dependencies and, in turn, all of their dependencies. This process is written so that it can output extensive debugging information, showing you which modules convinced py2exe to include which other modules. To display this debugging trace, run the py2exe "module finder" code standalone with at least one -d option to turn on "debugging":

Best practice to install dependencies?

I am thinking of a good way to ship my application which is a python package. Installing my package is easy making use of pythons distutils package.
The trouble comes with the dependencies my package relies on. If the dependencies are python packages I can deal with them easily again using distutils, but non python packages? Some of them even need a lot of care while building and installing them since very special compiler flags need to be set and so forth...
If I want to automate the installation procedure for the user what is the best way to go about it?
Writing a make file that downloads and installs the dependencies
Write a script that installs the dependencies
No automation is best. simply write a manual that tells the user how to install the
dependencies
???
Thx in advance for any answer or suggestion
We have a project named Kivy ( http://kivy.org/ ), that have exactly the same issue. At the early stage, we've done a all-in-one package that include every setup of every dependencies. However the user was having a lot of "Next >" button to click... for every deps (Windows). So now, we have managed to take care ourself of the dependencies.
Except linux related (since all our deps are already packaged on "linux"), we have taken the approach of managing what we named "portable-deps" zipfile for each platform. And then, we have a script that:
Download the portable-deps zip
Include the latest version of our project
Add launcher script in the root directory
Zip the root directory, and rename the zip to project--.zip
With a special case for MacOSX, where the zip is a dmg with a little UI.
The good part is that the user don't have to care about deps, and developers know exactly what binaries are delivered with the project :)
For information, we have build_portable commands for distutils :
https://github.com/kivy/kivy/blob/master/kivy/tools/packaging/osx/build.py
https://github.com/kivy/kivy/blob/master/kivy/tools/packaging/win32/build.py
The most important thing to help you decide is to consider your audience.
Are they technically-inclined and likely to be comfortable following instructions specifying how to build the dependencies themselves? If so, go with (3). If not, writing a python or shell script, or a makefile to automate the task may be the way to go. Pick whichever you feel most comfortable writing.

Categories