How to import a 3rd party Python library into Bluemix? - python

My Python app needs web.py to run but I'm unable to figure out how to get it up to bluemix. I see no options using cf push. I tried to "import web" and added some additional code to my app without success.
When I push my Python app to bluemix without web.py it fails (naturally) since it does not have what it needs to run.
I'm sure I'm just missing an import mechanism. Any help?

I recommend that you try out this starter template on GitHub. It is enabled with a deploy to Bluemix button that automatically creates a python runtime and postgress database with Django installed. https://github.com/fe01134/djangobluemix
The project includes the requirements.txt file to ensure you have the right dependencies and also the .settings file to read the database user id and password from VCAP Services. It also leverages Declared services in the manifest file to create a database service for you.
Here is a YouTube Tutorial on how to deploy Python app on Bluemix https://www.youtube.com/watch?v=qIcHQQNUmlE&list=PLvsG7O_a5F2dAjsNp6aRACP6vkqdgsZ33&index=5

The cause for this problem was that I was not correctly telling my Python app the needed configuration information when I pushed it out to Bluemix.
What I ended up having to do was add a requirements.txt file and a Procfile file into the root directory of my Python application, to draw that connection between my Python app and the needed libraries/packages.
In the requirements.txt file I specified the library packages needed by my Python app. These are the file contents:
web.py==0.37
wsgiref==0.1.2
where web.py==0.37 is the version of the web.py library that will be downloaded, and wsgiref==0.1.2 is the version of the web server gateway interface that is needed by the version of web.py I am using.
My Procfile contains the following information:
web: python .py $PORT
where myappname is the name of my Python app, and $PORT is the port number that my Python app uses to receive requests.
I found out too that $PORT is optional because when I did not specify $PORT my app ran with the port number under the VCAP_APP_PORT environment variable for my app.
From there it was just a matter of pushing my app out to Bluemix again only this time it ran fine.

just use pip freeze
pip freeze > requirements.txt
add the requirements.txt to your project
when deployed it will automaticly install the dependency

Related

How to deploy a Node.js application with a child process (python) in Heroku?

I'm trying to deploy a Node.js application with a child process that runs a machine learning algorithm. I can use this locally, but when I try to run at the Heroku server I recive some messages calling that are some libraries missing, like bellow:
ModuleNotFoundError: No module named 'pandas'
I tried to create manually the requirements.txt and put the necessary libraries there:
pandas
pymongo
dnspython
scikit-learn
scipy
selenium
webdriver-manager
textblob
But it doesn't work. Do I need to do some extra configuration?
Thank you so much for your help!
The way your Heroku dynos run your software is through something called a buildpack.
When you deploy an application to Heroku, it looks at your code and tries to figure out which programming language you are using, then based on that, will run your app using the corresponding buildpack.
For example, if you deploy an app to Heroku and the app has a package.json file in the root of your project directory, Heroku will assume your app is a JavaScript app and use the Node.js buildpack.
Buildpacks contain a number of pre-installed dependencies. For example, the Node.js buildpack contains node (so you can run your JavaScript code) as well as a number of Linux dependencies so that your app will be able to install common libraries/tools that might rely on them.
But... One downside of this buildpack strategy is that if you're deploying a Node.js app, for example, the default Node.js building will NOT come with Python and the various Python library dependencies installed. This is because Heroku supports a lot of different programming environments, and it would be slow/complex if there was just a single buildpack that had EVERYTHING installed. It'd be crazy!
So what you need to do, in your case, is use multiple buildpacks! Heroku has a way for you to enable multiple buildpacks for your app so that your app can have the Node.js dependencies as well as the Python dependencies, for example!
This article on Heroku's documentation site explains how to use multiple buildpacks for a given app.
Here are the specific instructions for simplicity's sake:
# This command will set your default buildpack to Node.js
$ heroku buildpacks:set heroku/nodejs
# This command will set it up so that the Heroku Python buildpack will run first
$ heroku buildpacks:add --index 1 heroku/python
By doing the above, you'll be able to have Heroku install your Python dependencies via a traditional requirements.txt file like you would with any normal Python application.

Deploying Flask app on Heroku with local dependencies

I created a Flask app that I would like to deploy on Heroku (or similar platform). The process I've followed in the past is to creating a 'requirements.txt' file with the packages from my venv and Heroku installs them in the virtual server. The issue I am having is that I have changed some of the code in the Flask library to get the app to work like I want. Is there some way to load local packages? Should I remove it from my venv/lib folder and add it to my main folder?
Thanks,
Marc
You can use git for that.
Please check this link. https://devcenter.heroku.com/articles/python-pip#local-file-backed-distributions
You would have to out the local library in the git repo and then update your requirements.txt file

"ModuleNotFoundError: No module named 'django'" when trying to deploy Django server on Azure

After I tried to deploy my Django website on Azure, I got an error saying:
ModuleNotFoundError: No module named 'django'
I added a requirements.txt in the root directory of my Django project, am I missing anything else? I've tried to install Django from Kudu BASH but it gets stuck on "Cleaning Up".
Here is the full error: https://pastebin.com/z5xxqM08
I built the site using Django-2.2 and Python 3.6.8.
Just summarized as an answer for other people. According to your error information, I can see that you tried to deploy your Django app to Azure WebApp on Linux based on Docker. So there are two offical documents will help as below.
Quickstart: Create a Python app in Azure App Service on Linux
Configure a Linux Python app for Azure App Service
The error ModuleNotFoundError: No module named 'django' indicated that there is not django package installed on the container of Azure Linux WebApp.
Due to the content of Container characteristics of #2 document above as below,
To install additional packages, such as Django, create a requirements.txt file in the root of your project using pip freeze > requirements.txt. Then, publish your project to App Service using Git deployment, which automatically runs pip install -r requirements.txt in the container to install your app's dependencies.
So the possible reason is the requirements.txt file not in the corrent path of your project or container after deployed, which path should be /home/site/wwwroot/requirements.txt on the container or the root of your project like the offical sample Azure-Samples/djangoapp on GitHub.
I had the same problem. requirements.txt was in my repository, but randomly I started getting the same error ModuleNotFoundError: No module named 'django' after changing a setting or restarting the service or something. Nothing I could do, change the setting, or reboot repeatedly fixed it. Finally what worked for me was this:
Make a small change to the code and commit it, and push it up to the app service.
This fixed it for me. It has happened a couple times now and every time this solution has worked for me. It seems like the App Service sometimes gets in this state and needs to be jostled with this trick?

How do you pip install libraries to the 'antenv' venv on an Azure Web App?

I am trying to deploy a Flask app to an Azure Web App (Linux, python3.7 runtime) using FTP.
I copied the "application.py" over and a "requirements.txt", but I can see in the logs that nothing is being installed.
The Web App is using an 'antenv' virtual environment but it won't install anything. How do I add libraries to this 'antenv' virtual environment?
Yes, I see that you have resolved the issue. You must use Git to deploy Python apps to App Service on Linux so that your dependencies in requirements.txt are installed (root folder).
To install Django and any other dependencies, you must provide a requirements.txt file and deploy to App Service using Git.
The antenv folder is where App Service creates a virtual environment with your dependencies. If you expand this node, you can verify that the packages you named in requirements.txt are installed in antenv/lib/python3.7/site-packages. Refer this document for more details.
Additionally, Although the container can run Django and Flask apps automatically, provided the app matches an expected structure, you can also provide a custom startup command file through which you have full control over the Gunicorn command line. A custom startup command is typically required for Flask apps, but not Django apps.
Turns out I had to run these commands and do a git push while my local venv was activated. At that point I saw azure start downloading all the libraries in my requirements.txt

Running simple python script continuously on Heroku

I have simple python script which I would like to host on Heroku and run it every 10 minutes using Heroku scheduler. So can someone explain me what I should type on the rake command at the scheduler and how I should change the Procfile of Heroku?
Sure, you need to do a few things:
Define a requirements.txt file in the root of your project that lists your dependencies. This is what Heroku will use to 'detect' you're using a Python app.
In the Heroku scheduler addon, just define the command you need to run to launch your python script. It will likely be something like python myscript.py.
Finally, you need to have some sort of web server that will listen on the proper Heroku PORT -- otherwise, Heroku will think your app isn't working and it will be in the 'crashed' state -- which isn't what you want. To satisfy this Heroku requirement, you can run a really simple Flask web server like this...
Code (server.py):
from os import environ
from flask import Flask
app = Flask(__name__)
app.run(environ.get('PORT'))
Then, in your Procfile, just say: web: python server.py.
And that should just about do it =)
If you use free account [unverified*] on Heroku (so you cannot install addons), instead of using "Heroku scheduler", use time.sleep(n). You don't need Flask or any server in this case, just place script, say, inside folder Scripts (in default app/project by Heroku) and add to Procfile: worker: python script.py. Of course you replace script.py with Path to your script, including name, ex. worker: python Scripts/my_script.py
Note: If your script uses third-party modules, say bs4 or requests, you need to install them in pipenv install MODULE_NAME or create requirements.txt and place it where manage.py, Procfile, Pipfile, (etc) are. Next place in that requirements.txt:
requirements.txt:
MODULE_NAME==MODULE_VERSION
You can check them in pip freeze | grep MODULE_NAME
Finally deploy to Heroku server using git and run following command:
heroku ps:scale worker=1
That's it! Bot/Script is running, check it in logs:
heroku logs --tail
Source: https://github.com/michaelkrukov/heroku-python-script
unverified* - "To help with abuse prevention, provisioning an add-on requires account verification. If your account has not been verified, you will be directed to visit the verification site." It redirects to Credit Card info. However you can still have Free Acc, but you will not be able to use certain options for free users, such as installing addons:https://devcenter.heroku.com/articles/getting-started-with-python#provision-add-ons

Categories