How to automate linting, testing and file generation in python? - python

I'm sure this is answered somewhere else, but I can't for the life of me find it!
I have some python files and I want to automate the linting, testing and running of them. This is going to be part of a CI dependency chain, and I know how I would do this in e.g. Jenkins.
However the problem is that I would like developers to be able to run the above change locally before they push changes to the remote repository. I have come from a C++ team where we used team and I'm used to running something like:
scons test
That would run the linter, the tests, and maybe then output some files.
How can I replicate this functionality on a Python code base? I realise I could do this with a bash script or something, but it seems that there should be a standard build tool to do this. Thanks!

Related

How to make an installation interface for an application written in python

I'm working on an app, it's in the final stages of development, and I want to help design an installation interface for it, can anyone guide me?
This question largely depends on how thorough you want your installer to be, since installing a program basically translates to embed this complex piece of software on my pc for me to use hassle-free. To make a common one click installer, it will be a long process that is explained better here. I wont get too deep into this because there's many builders and tools that do the heavy lifting and simplify this process significantly. Just an example anyways, these can be built in Visual Studio and are formatted as .msi or .exe, or packaged up as self-extracting in InstallSheild. On a side note mac/linux installations work very differently. This is for Windows only!
If you are leaning towards making a much more indie-friendly installer that just moves a file to a location, a simple Windows VB program that is ran with admin privileges would do the trick. Simply fetch some files from a http source to a location on your computer, and VB supports a lot of very nice GUI elements to guide the user through the process.
As ironic as this sounds, you could even make the downloader in python and bundle that as an exe installer. It could utilize PyQT5, or just stay plain-jane command line and spit out the log as it executes.
At the end of the day, a bare-bones method would just be to host your application through a github repo and have clients git clone, or add your code to pypi.

How do I make a macOS app out of my Python program?

I've made this question because I had to go through the whole process of creating my own application using Apple's somewhat lacking documentation, and without the use of py2app. I wanted to create the whole application structure so I know exactly what was inside, as well as create an installer for it. The latter of these is still a mystery, so any additional answers with information on making a custom installer would be appreciated. As far as the actual "bundle" structure goes, however, I think I've managed to get the basics down. See the answer below.
Edit: A tutorial has been linked at the end of this answer on using PyInstaller; I don't know how much it helps as I haven't used it yet, but I have yet to figure out how to make a standalone Python application without the use of a tool like this and it may just be what you're looking for if you wish to distribute your application without relying on users knowing how to navigate their Python installations.
A generic application is really just a directory with a .app extension. So, in order to build your application, just make the folder without the extension first. You can rename it later when you're finished putting it all together. Inside this main folder will be a Contents folder, which will hold everything your application needs. Finally, inside Contents, you will place a few things:
Info.plist
MacOS
Resources
Frameworks
Here you can find some information on how to write your Info.plist file. Basically, this is where you detail information about your application.
Inside the MacOS you want to place your main executable. I'm not sure that it matters how you write it; at first, I just had a shell script that called python3 ./../Resources/MyApp.py. I didn't think this was very neat though, so eventually I called the GUI from a Python script which became my executable (I used Tkinter to build my application's GUI, and I wrote several modules which I will get to later). So now, my executable was a Python script with a shebang pointing to the Python framework in my application's Frameworks folder, and this script just created an instance of my custom Tk() subclass and ran the mainloop. Both methods worked, though, so unless someone points out a reason to choose one method over the other, feel free to pick. The one thing that I believe is necessary, is that you name your executable the SAME as your application (before adding the .app). That, I believe, is the only way that MacOS knows to use that file as your application's executable. Here is a source that describes the bundle structure in more detail; it's not a necessary read unless you really want to get into it.
In order to make your executable run smoothly, you want to make sure you know where your Python installation is. If you're like me, the first thing you tried doing on your new Mac was open up Terminal and type in python3. If this is the case, this prompted you to install the Xcode Command Line tools, which include an installation of Python 3.8.2 (most recent on Xcode 12). Then, this Python installation would be located at /usr/bin/python3, although it's actually using the Python framework located at
/Applications/Xcode.app/Developer/Library/Frameworks/Python3.framework/Versions/3.8/bin/python3
I believe, but am NOT CERTAIN, that you could simply make a copy of this framework and add it to your Frameworks folder in order to make the app portable. Make a copy of the Python3.framework folder, and add it to your app's Frameworks folder. A quick side note to be wary of; Xcode comes packaged with a lot of useful tools. In my current progress, the tool I am most hurting for is the Fortran compiler (that I believe comes as a part of GCC), which comes with Xcode. I need this to build SciPy with pip install scipy. I'm sure this is not the only package that would require tools that Xcode provides, but SciPy is a pretty popular package and I am currently facing this limitation. I think by copying the Python framework you still lose some of the symlinks that point to Xcode tools, so any additional input on this would be great.
In any case, locate the Python framework that you use to develop your programs, and copy it into the Frameworks folder.
Finally, the Resources folder. Here, place any modules that you wrote for your Python app. You also want to put your application's icon file here. Just make sure you indicate the name of the icon file, with extension, in the Info.plist file. Also, make sure that your executable knows how to access any modules you place in here. You can achieve this with
import os
os.chdir('./../Resources')
import MyModules
Finally, make sure that any dependencies your application requires are located in the Python framework site-packages. These will be located in Frameworks/Python3.framework/Versions/3.X.Y/lib/python3.x.y/site-packages/. If you call this specific installation of Python from the command line, you can use path/to/application/python3 -m pip install package and it should place the packages in the correct folder.
P.S. As far as building the installer for this application, there are a few more steps needed before your application is readily downloaded. For instance, I believe you need to use the codesign tool in order to approve your application for MacOS Gatekeeper. This requires having a developer license and manipulating certificates, which I'm not familiar with. You can still distribute the app, but anyone who downloads it will have to bypass the security features manually and it will seem a bit sketchy. If you're ready to build the installer (.pkg) file, take a look at the docs for productbuild; I used it and it works, but I don't yet know how to create custom steps and descriptions in the installer.
Additional resources:
A somewhat more detailed guide to the anatomy of a macOS app
A guide I found, but didn't use, on using codesign to get your app past Gatekeeper
A RealPython tutorial I found on using PyInstaller to build Python-based applications for all platforms

New to VSCode, Python - some env questions

I went through a few articles about how to setup VSCode to work with python for the first time, and I'm very confused.
To me, VSCode seems messy - so many json settings which I manually need to configure...
Anyway, I now have 3 ways of running my python script:
F5, which runs via the terminal
Run (Ctrl+Shift+D), via Watson
Run via Code Runner
I don't understand the differences between the different methods of running my script.
Can someone please give me a short intro?
Thanks!
Terminal:
Terminal is fast and efficient to use. It also provides detailed information about the errors in your programs. And it is widely used buy a lot of Python programmers.
Watson:
It provides developer tools for more advanced users. You can get more information about that from here. Not recommended for a beginner.
Code Runner:
Code runner is also used to see the the results of the programs but it is read only, and there can be some problems with that. As an example if you are building an application that takes the input from the user in Python, you can't do that via code runner.
So which one is the best?
I would recommend using the Terminal (F5), because it is good for small programs, you get detailed information about your apps, the problems with your apps and some other options too. It is also fast and easy to use for a beginner.
Hope it's clear now.

Do i make an egg when i want to deploy a python script with custom imports to a new host machine?

Kind of new to python. I have been working through some development and have been working on some scripts in pycharm. Anyways, I have been doing a lot of custom installs on my local machine leveraging things like pythoncom, or pyHook. Those may not be on my target machine so instead of just running the python script, I am thinking I should turn it into an egg file that i just run on the machine instead?
I figure it would just do what a jar does and builds all the imports etc into the egg so it is self contained and will run correctly.
Is that true? Is that the best course of action? I was tempted to do something like npm install but for python, though a lot of things within my defined modules arent really found in pip but instead on sourceforge.
How would I best do this approach? Is making an egg the way I am suppose to do it, or will I want to build some sort requred structure (sort of like a packages.json file for npm and then just run that when I clone a repo)?
I was hoping to do it all by way of a repo, but i dont know what the python development / distribution standard is.

Tools for continuous integration with Python

My project is based on only Python code.We are using multiple tool for pylint,profiler for improving the code quality.So each developer need to run individually run this Tool.I am planning to integrate all tools into single script or tool. We generally integrate new tool in hudson build tool in c++ But I am not sure is it possible in Python as I recently move to Python.So I have very basic question.
I have searched into Python and found many build tool But I could not figure out any one which can use be for integrate to plug-in.
Do we have any tool which can do our purpose and not require to have build functionality.
Somebody suggest me to write shell script rather than look for any tool.
As of now, we are not using any build tool in Python.
You'd better follow #WoLpH comment on how to configure hudson. And I strongly advice you to switch to Jenkins, as it has a more active developer community than hudson.
About using a build tool in python, it depends on the complexity of the project you want to deploy:
if it is a project that has only simple python dependencies, you'd better use virtualenv ;
if you need to checkout some private repositories, or make more complex arrangements on your repositories (or if you don't want to mess your shell's environment), then zc.buildout is for you ;
if what you want is something closer to Makefile, but that you can use in a more extensible and pythonic way, then you'd better have a look at scons
In either way, you'll need to make a setup.py, and add support for unit testing. For unit testing in python, you'd better have a look at nose.py.
Once you chose your weapons and configured your environment, jenkins (or hudson if you want to keep the old one) is pretty easy to configure.

Categories