Deploying a server-less Django app? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Originally the requirements were to have an outward facing application and we were going to use a server. However, now all the code is done and long term vision has changed... The requirements are to have an inward facing application without a server (physical or remote).
The team that will be using the application will be 5 users on the same network and each is to have the app individually installed on their desktop.
My question is what is the best way to deploy this application to everyone's machine as a package they can easily install themselves. How can the application always be running on everyone's machine without typing python manage.py runserver?

I had to do this exact same thing. My solution was to use PyInstaller, which bundles all your packages and files into one neat distribution folder with an executable file in it.
First, look at this link for a tutorial. To be honest, this alone did not work for me, but it could for you. So I will document some things to look out for down below and continue from here as if it runs perfectly for you.
After PyInstaller runs on your manage.py file, it will create three new objects for you: a dist folder, a build folder, and a myAppName.spec file. In the dist folder is an executable for the project. To deploy this project to other places, the only thing needed is that dist folder (or even just the myAppName folder inside that). Simply zip it up and send it off.
The executable inside the dist folder does not run just by clicking on it though, it still requires you to type python manage.py runserver. To get around this, I made a shortcut to the executable and modified it's target. So if your project's path is C:\path\to\proj, the target of the shortcut will be C:\path\to\proj runserver. This should allow you to double click the shortcut and run the local server. If you also wanted to open a web browser on the same click, make a batch file. If you don't like having the console open, try TrayIt.
If you want to re-run PyInstaller on your project, get rid of those dist and build folders for speed, but keep that .SPEC file and run pyinstaller on that instead. It can help you be more specific about the process if you care.
Issues to look out for:
As mentioned above, when I ran PyInstaller on my Django app I had a few of problems to work through.
Whenever I ran my new executable, the console complained about "Module not found" errors. Here is my solution to these errors. I eventually had to add more hidden imports than my answer shows. Ask and you shall receive.
PyInstaller was supposed to package all templates (html files), but it did not. I had to manually add all files to the dist\myAppName\django\contrib\admin\templates folder.
PyInstaller was also supposed to grab all outside modules you use, but it did not. To use the missing module, I had to grab the module's folder from where I downloaded it and manually add it to my dist/myAppName folder. Just pasting it there worked out fine.
PyInstaller is also supposed to get rid of unused imports, but it did not. I had to add a lot of modules to the excludes list in my .SPEC file so that my dist folder wasn't huge.
Let me know if your PyInstaller experiences are better than mine. I think my issues may have been isolated instances.

You could use py2exe for Windows or py2app for OSX to convert your python application to an executable to start your Django application.

Related

How to bundle Python apps with asset dependencies for end users?

I have a Tkinter app that uses images included in the same folder as the .py file. pyinstaller script.py produces an executable that runs but does not open any windows. This is because it is looking for images that don't exist in the same subdirectory. When I copy the important images to the dist folder Pyinstaller creates, the application runs correctly.
However, I would like to have a single executable that I can share with other users that doesn't also require them to have the images stored. The images should be bundled with the software somehow, like how commercial software (usually) doesn't require you to download assets separately from the program itself.
Is there a way to bundle Python programs and the assets they use into single-click applications?
Note that I am using Python 3 on Linux Mint. I am also something of a novice, so don't be surprised if I'm missing something obvious here.
It appears I've solved my own problem.
Instead of having the images included in the same folder as main.py and using the resulting short relative filepath to reach them, install the images in an appropriate space in the system directory tree (I used /home/$USER$/.$PROGRAMNAME$/) and have the program access the files using the absolute path to that directory. This will allow you to copy the program anywhere on your computer you want and have it run without a problem.
However, if you want to share it with someone else, you'll need to also include an installation script that places the assets in the correct directory on their computer.

How to build a customizable and changeable python module inside an executable?

I've built a project with Python in which a module of functions can be changed by the user. More specifically, functions can be added or deleted inside this module by other processes in the application. Now I have just converted the whole project into an executable file using auto-py-to-exe to run it through a console window, instead of running it through VS Code for instance. I can't change the module if it was not added as an additional file in auto-py-to-exe rather can the application use this module if I do add it as an additional file.
My question is: how can I turn this project into an executable with the possibility of changing this module by the program itself?
This may be tricky, but it is possible with tools like pyinstaller when you bundle your app as a directory, rather than a single file executable. The source files (albeit compiled) will be present in the directory.
In principle, you could edit files in a single-file executable, but it's probably more trouble than it's worth and may be blocked by permissions and other issues.
You could also design your software to read a file from a predefined location (or the bundle directory -- anywhere accessible by the user) and simply exec the string of the code to 'load' it. It could look more or less like an extension/scripting system for your program. One example comes to mind: the iterm2 software Python API.
It should go without saying that you must trust your users/inputs if you give them the ability to arbitrarily change the software code.

How to encrypt and convert my python project to exe

I have created python desktop software. Now I want to market that as a product. But my problem is, anyone can decompile my exe file and they will get the actual code.
So is there any way to encrypt my code and convert it to exe before deployment. I have tried different ways.
But nothing is working. Is there any way to do that?.Thanks in advance
This link has most of the info you need.
But since links are discouraged here:
There is py2exe, which compiles your code into an .exe file, but afaik it's not difficult to reverse-engineer the code from the exe file.
You can of course make your code more difficult to understand. Rename your classes, functions to be non-sensical (e.g. rename print(s) to delete(s) or to a()) people will have a difficult time then.
You can also avoid all of that by using SaaS (Software as a Service), where you can host your code online on a server and get paid by people using it.
Or consider open-sourcing it :)
You can install pyinstaller per pip install pyinstaller (make sure to also add it to your environment variables) and then open shell in the folder where your file is (shift+right-click somewhere where no file is and "open PowerShell here") and the do "pyinstaller --onefile YOUR_FILE".
If there will be created a dist folder, take out the exe file and delete the build folder and the .spec I think it is.
And there you go with your standalone exe File.

Proper way to run a PyCharm project

This is a seemingly simple problem but it proves to be harder than expected.
I've created a project in pycharm in the following layout
bin
main
helpers
userhelper
models
user
session
tests
userTest
in my main I run the code that calls everything and this works like a charm in pycharm. Now I want to run this on a server and start it with cron. How do I start this from cron while keeping all the module references in place?
I guess I need to add the root of my project to the python path. To do this I added the following bash script to invoke my project with:
PYTHONPATH="${PYTHONPATH}:/home/steven/projectX"
export PYTHONPATH
python bin/main.py
But this does not seem to do anything, what would be the best way to periodically run the bin/main.py within this project and have all my modules and things like 'ConfigParser.RawConfigParser().read(os.path.abspath("../configuration.cfg"))' in place relative to my project?
EDIT: I am not trying to fix my imports or debugging my code, I have a large project in pycharm that runs a simulation that I want to invoke on the server an maintain within my development setup. The question is how do I run this in the same way pycharm does?
It sounds like you're interested in making a distributable Python package. You should read through the tutorial here. Ultimately, you're going to want to write a setup.py (sure you could call it something else, but it's like renaming self -- why do it?) that will configure your project. Now a word of advice since I've seen many people go down a wrong path here. You NEVER want to modify your PYTHONPATH directly. It might be the quickest solution to get something up and working, but it WILL cause lasting problems.
So I've found the way of dealing with mu issue here. The best option would obviously be to create a distributable python package and use this. However since I am developing a simulation that has loads of features and desired outcomes there is a lot of tuning going on and having to create a package every time I want to run the project is a bit out of the scope for my project.
What I do want to do is being able to run the files in a similar way to how I am doing this on my development machine with PyCharm. The way I resolved this is to have my main.py in the root of my project and to have all references relative to the executed file. This means my userhelper looks for the datastore as follows:
path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
"resources/" + settings['datastore_filename']
This resolves my issue of not being able to run my project on the server the same way it runs on my PC.

How to distribute a stand-alone python application?

I want to distribute my Python application to co-workers for them to use. The application will on be run on Linux systems, but the users do not have admin privileges so cannot install my application's module dependencies. I would the users to be able to untar my application and then run my main.py script. Running another one-time 'install'-type script is okay, but not much else.
PyInstaller is close to what I want. Except I would like to distribute the source code of my application as well. So the application should be stand-alone and self-contained (with or without the python interpreter is fine, preferably with), but users should be able to make small changes to the code and rerun the application. My ideal solution is to create some sort of compressed/compiled archive of all my applications module dependencies and distribute that with my application. It doesn't have to be all dependencies, but at least the non-standard packages. The application will then import modules from this archive instead of the user's PYTHONPATH.
I tried virtualenv, but having the users source the activate script was a little too much. I've been looking into numerous other solutions, but I can't find one that works for me.
Why don't you create a directory with the interpreter you want to use, add in any modules etc. Then drop in a bash script, say run.sh which calls the program. It can launch your chosen interpretter with your python files, arguments etc.
Any source files can remain this way and be edited in place. You could tar and distribute the whole directory, or put in something like git.
One approach is to use virtualenv. It is designed to create isolated python environment and does a good job at it. It should be possible (link to package the virtualenv with your app with some effort. However virtualenv is not designed for that so it's not as easy as it could be.
package-virtualenv GitHub project might also help.

Categories

Resources