Use pipenv with django heroku - python

So I began to code a project with python, and I was using a tutorial that told me to use a pip environment as my virtual environment. A problem arose, however, when I performed the git push heroku master command. It could not find the package django-heroku!
I was confused, because when I ran python manage.py runserver, the server on my computer ran. I then changed to a python environment, which was located in the directory I was pushing to heroku. The problem was solved! The virtual environment, and consequently the installed packages, were inside the directory being pushed to git, and I could use them in my website!
But the question still remains: can you use a pip environment for a django project being pushed to git? Thanks!

You should not include python packages within your repo and push them to Heroku. Check https://devcenter.heroku.com/articles/python-pip. You only need a requirements.txt file in your root directory. Heroku will install the packages automatically for you.

Related

How can we deploy a Machine Learning Model using Flask?

I am trying, for the first time ever, to deploy a ML model, using Flask. I'm following the instructions from the link below.
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c
I created three separate and distinct .py files named 'model.py', 'server.py', and 'request.py'. I open my Anaconda Prompt end entered this: 'C:\Users\ryans>C:\Users\ryans\model.py'
Now, I get this.
I definitely have Numpy installed! Something must be wrong with my setup, or maybe the way I am starting the process is wrong, but I'm not sure what the issue is. Has anyone encountered this problem before.
I would suggest the following steps since you mentioned its your first time and when deploying a project for experimenting, it's good practice to put it in a virtual environment, which we can do with the virtualenv tool.
Assuming you already have pip installed, install virtualenv:
C:\> pip install virtualenv
Create a fresh working directory, switch to it and clone the repository from Github mentioned by the author of the article:
C:\your-working-directory\> git clone https://github.com/vyashemang/flask-salary-predictor.git
Start virtualenv in the repository's top directory
C:\your-working-directory\flask-salary-predictor\> virtualenv env
Activate the created batch file, \env\Scripts\activate.bat:
C:\Users\'Username'\venv\Scripts\activate.bat
Now in the virtual environment, install all requirements:
(env) C:\your-working-directory\flask-salary-predictor\> pip install -r requirements.txt
You can now run the model.py like you have above. I'd suggest that you run the server.py using gunicorn or on Windows use waitress according to this post so that instead of running request.py every time you want to send a request, you can use Insomnia or Postman to send API requests.

Heroku Django Virtual Environment source code modification

I managed to deploy my django app (with mongodb as database) on heroku.
But I need to modify some source code in django package in the virtual environment.
How can I access to virtual environment created by heroku from requirements.txt ?
Or maybe how can I upload directly the virtual environment to heroku and make sure that my GIT django app is working on it?
thank you
If your question is how would you know git dependencies match your local environment, first you should freeze your installed modules:
pip freeze > requirements.txt
Then include this file in the root directory of your app which will be deployed to heroku. After it is pushed to git, take a look at the commands run and verify the modules are installed.

why doesn't pip freeze > requirements.txt output Django?

I'm trying to deploy my Django site on Heroku, and thus, I need a requirements.txt file with the necessary packages that Heroku needs to install for me. I understand Django is a necessary package to be installed. Unfortunately, Django isn't included in the file when I run pip freeze > requirements.txt. Why is this? I'm not sure what to show you so you can tell me what's going wrong. Let me know and I'll add it. FYI the site hosts just fine on my local computer, so Django is definitely installed.
Sounds like you are working in a virtual environment, yet your Django dependency is installed globally. Check which Python packages are installed globally and uninstall Django (you probably don't need it globally). Then install it into your virtual environment. Now the freeze command should output Django as well.
General note: Most packages should be installed into your project virtual environment. There are only few packages where it makes sense to install them globally (eg aws management tools).

Pip Wheel Package Installation Fail

I try to run pip wheel azure-mgmt=0.20.1, but whenever I run it I get following pip wheel error, which is very clear:
error: [Error 183] Cannot create a file when that file already exists: 'build\\bdist.win32\\wheel\\azure_mgmt-0.20.0.data\\..'
So my question is where or how I can find that path? I want to delete that existing file. I have been searching my local computer, searched for default path in Google, but still didn't find any solution.
Also is it possible to tell pip wheel to output full log? As you can see that full error path is not displayed. I'm using virtualenv.
We can see the description of virtual env at the official python guide:
To help manage external package dependencies, Azure Git deployment supports the creation of virtual environments.
When Azure detects a requirements.txt in the root of the repository, it automatically creates a virtual environment named env. This only occurs on the first deployment, or during any deployment after the selected Python runtime has changed.
You can directly modify the dependencies and versions of them in requirement.txt, then deploy your python app to Azure Web App via git, Azure will update the python packages automatically. You can check the packages in the virtual env folder which path is env\Lib\site-packages in the root directory of your site. You can login on the kudu console of your site to check your files of you site online, the URL should be: https://{your_site_name}.scm.azurewebsites.net/DebugConsole .
Additionally, according your description, it seems that you use the global python environment to run pip install command which may directly install packages in your global python environment. To install packages in your virtual env, you need to run the similar command env\scripts\pip install -r requirements.txt in your root directory of your application. Please refer to Web app development - Windows - command line for more information.
have you tried uninstalling and reinstalling?
I tried pip wheel azure-mgmt and that installed -0.20.1 for me.
The directory for mine is /Users/me/wheelhouse, so you could look there. I found that in the initial log of the build.
#Amir,
One option is that you could generate the requirement.txt file and remove your virtual environment if you used Visual studio to develop your application. Then you can add a new virtual environment for your project and install all packages from requirement.txt file. Or after removed your virtual environment, you can try pip wheel azure-mgmt command.
And another option is that you can follow this similar issue:https://vilimpoc.org/blog/2014/01/18/time-robbing-python-errors/
The blogger modified the LOCALAPPDATA path to resolve this issue. Please try it.

How do you delete, clean or clear a virtualenv in Heroku?

I have a Heroku Python project that is emitting errors that I can't reproduce locally. I suspect that the Python virtualenv in the Heroku deployment is corrupted in some way.
Is there a way to clean the Python virtualenv in a Heroku project and start fresh?
See Heroku docs. Create a runtime.txt file with a different version of Python than your current version and push to heroku. This will purge your appication's build cache and virtualenv. Then you can revert to your previous Python version in the runtime.txt and push that up to Heroku, giving you both your desired Python runtime and a clean virtualenv.

Categories

Resources