I have setup a python project in Visual Studio 2017 Enterprise (Version 15.4) as part of a mixed-project solution also containing various C#(.Net 4.6.2, .Net Core 2.0 and Xamarin) projects. During the build I want to generate several .py source code files using some self-written C# tool which is also part of the build. Additionally all .py files listed as "Compile" items in the .pyproj file should be compressed into a .zip file and written to some output directory.
In my .pyprof MSBuild file I craeated two "custom build step" targets like so:
<Target Name="MyPreBuildStep" BeforeTargets="Build">
Call self-written py-Code generator
</Target>
<Target Name="MyPostBuildStep" AfterTargets="Build"
Inputs="#(Compile)"
Outputs="$(OutputPath)\$(PythonProjectName).zip">
<Zip ZipFileName="$(OutputPath)\$(PythonProjectName).zip"
WorkingDirectory="$(MSBuildProjectDirectory)\..\" Files="#(Compile)" />
</Target>
The Zip Task being imported from the MSBuildCommunityTasks.
If I right-click on the project in Visual Studio and hit "Rebuild" everything works fine. The py-code is generated in advance and all #(Compile) items are put into the .zip file.
Unfortunately the normal "Build" NEVER invokes the above targets. So with my current setup, I can build my 40-projects containing solution just by hitting "Build" BUT the python projects needs an extra-invitation by hitting "Rebuild". Very annoying.
I tried to investigate the contents of "Microsoft.Python.Tools.Web.targets" and "Microsoft.Python.Tools.targets". Somewhere inside these files a comment says:
"The Build target is invoked as part of the Publish phase."
hmmm.
Can somebody tell me how to specify some simple custom build steps in a .pyproj that just work during a normal "Build"?
Thank you.
Build is only invoked when required, and this is determined based on comparing the last modified times of input and output files.
Mostly these are automatically collected, but in your case the output file is not recognized and so does not influence whether it needs to be built. (If you inspect Microsoft.PythonTools.targets and find the BuiltProjectOutputGroup and SourceFilesProjectOutputGroup targets, these are the ones that provide the files.)
In this case, you should only need to add the ZIP file as an output file so that it is checked. You can do this with:
<ItemGroup>
<OutputFiles Include="$(OutputPath)\$(PythonProjectName).zip">
<Visible>false</Visible>
</OutputFiles>
</ItemGroup>
Note that it needs to be available without running the build target, so you have to be able to compute the path statically, but now the project should rebuild if this file does not exist or any of the source files are newer than it.
Related
I have finished my python project and now want to transfer the project into one file, so a user can just double click it and doesn't have to compile it first.
Therefore, I wanted to know if this is possible with python.
I 've read that you can convert a single python script into an executable file using pyinstaller. But in my case I have many files in different folders and I want them to be include in the executable file because otherwise the programm doesnt work.
I also tried this via the auto-pyto-exe converter (https://github.com/brentvollebregt/auto-py-to-exe) but I didnt got the results I wanted.
Therefore, my question is, how can I convert my visual studio python-project with many different files and folders into one executable file, so a user can just double click the file to start it?
Edit
In the other folder are also .py files like some FileImport.py or View.py. I separated these files that the whole project looks cleaner.
The Folder structure looks as follows:
-Views
---MainView
---UpdateView
---AnotherView
-Controllers
---MainViewController
---UpdateViewController
-Model
---MainModel
I found the solution. As it appears the pyinstaller can find all dependencies when you compile your script via pyinstaller myscript.py. PyInstaller creates three different folders where all necessary files are located. In the dist folder one executable is located which can be used from computers without python installed.
I hope this helps somebody who has the same problem.
I have python.exe installed on my machine (several times) but no python.bat. I'm trying to build Chromium Embedded Frameworks and the master batch file says
#echo off
python.bat tools\gclient_hook.py
but python.bat not on my machine giving error 'python.bat' is not recognized as an internal or external command,operable program or batch file.
Is there a python scripting edition or something?
UPDATE: SO User R-nar was correct, needed stripping off .bat
This is not my code but part of Chromium Embedded Frameworks.
cef_create_projects.bat
Ok, so after fixing I am running into Python 2 errors. It looks like python.bat may have been an indirection mechanism to keep the Python 2 scripts working.
You have to use python.bat from depot_tools, see step 2 here:
https://bitbucket.org/chromiumembedded/cef/wiki/MasterBuildQuickStart#markdown-header-windows-setup
Download depot_tools.zip and extract to "c:\code\depot_tools". Do not use drag-n-drop or copy-n-paste extract from Explorer, this will
not extract the hidden ".git" folder which is necessary for
depot_tools to auto-update itself. You can use "Extract all..." from
the context menu though. 7-zip is also a good tool for this.
Check if have the the directory of python.bat in your environment path variable, otherwise python.bat must me in the current working directory.
I am trying to convert my python application to an exe. I have seen things like py2exe and cx freeze, but they only compile one single py file. Can anyone help me? Thank you
I currently use pyinstaller for building projects into single-executable files. These projects all contain multiple python (and some non-python) files that are all "built into" the exes.
That being said, even with multiple python files included, Marcus Müller is correct. There is one entry point for a given executable.
In summary, if you have multiple files as part of a single project, pyinstaller along with the other python bundlers will handle this scenario.
If you have multiple files and want them each to be their own executable file, you will need to treat each as its own 'project' and package each individually.
What platform(s) are you targeting? Can you describe the intended purpose of the files? Can you describe the intended usage of the files?
Posting an example of what you currently have, what behavior you are observing, and clarification on what is different between what you are expecting and what you are observing would definitely help others in guiding you towards the answer you desire.
Edit:
Well, I have a main python file that is referenced to a very brief config.py. The main file also accesses a few text files. Could I just combine the config and the main into one py file, and make that an executable, and will that executable still have access to the text files?
Your main python file would be your exe's entry point. If you import your config file into your main, pyinstaller should see the import and include it. On this line, verify your PATH environment variable, and insure your system knows where to find the bits it needs. If the text files are to be included as part of the built executable file, pyinstaller has the ability to include files into the build as well (example, include a database, a config, or a static data set). An example question describing including an icon file for a build: include-pyinstaller-icon
For example in PyInstaller, you can specify additional folders which should be involved in your executable file.
Due to a source control folder structure that is out of my control I have found myself in a situation where I need to keep my source python files in three separate locations up-to-date at all times. I am looking for the cleanest solution to accomplish this without having to resort to hacking it together.
I have all of my primary python source files and my Visual Studio 2012 Python Tools project files in a folder that is not published to the end user, but is managed via source control.
I have all of my python files updated into a separate folder that is published to the end users. Files contained in this folder are not ever executed, only used to publish into final builds.
I have a local folder that these python files will end up in if I download a published build. Unfortunately when executing the main application from the published build, this is the folder that the python files will be executed via.
Obviously this is a simplified explanation of the problem. However my goal is to cleanly be able to save a file while editing in Visual Studio 2012 Python Tools, and have it distribute to PublishFiles, and DestinationFiles. Very similar to how in any C++ project you would have an output directory.
My first reaction to this question would be to not work in this fashion, unfortunately I do not have any control over this situation, only need to develop a solution to the problem.
Thanks!
Yes, I read some of those .pyc questions, but still nothing.
I need to save a project to a CD and preferably I'd like to be able to run it right from there. Should I put the .pyc files in there or not?
No,
byte-code is not version compatible
Yes
the project is supposed to run with the same python version
.py files won't be changed any more in that release
it might be load faster
if smth doesn't suit, python will (need to) create new .pycs somewhere anyway
The latter one: Python will handle that and use a temp directory, won't it?
Answer : No Need to Include
You want to Compile & Run Means :-
Your question says, you are going to compile your python source code in another machine. When you compile your code, the ".pyc" file will be created. So, it is not necessary to include the ".pyc" file with your cd. If you put means, no problem, when you compile your source code in another machine, it will replace the ".pyc" file with the newly created ".pyc" file.
You want to Only Run Means:-
But, if you want to run without compile means, you should convert your program into executable. Executable file may be for windows or linux.
For Linux, to create executable file : Use "CDE" package.
Link: http://www.pgbovine.net/cde.html
For Windows, to create executable file : Use "Py2Exe" package
Link : http://www.py2exe.org/index.cgi/WorkingWithVariousPackagesAndModules
I hope this is the answer you want.
PYC is python bytecode making the original code run faster. You can omit it, since your code will still be able to run.
However if you need to run your code on different machines, probably without any python distribution. Take a look at
http://www.py2exe.org/ which is for windows only
http://www.pyinstaller.org/ which is for most systems
I personally worked with py2exe, it is simple to use. Although it makes fairly huge package, since it add all python packages required to run the original python script.
Hope this is helpful for You.