I'm running a Django app that I used to deploy to Heroku with Gunicorn without a hitch. By that I mean that I could run
foreman start
on my localhost and be able to understand whether the application would work in the production environment. I recently made the decision to move all of our project's applications into a subfolder named 'applications'. This meant editing my DJANGO_SETTINGS_MODULE environment variable among other files. After doing this, I experimented with foreman and gunicorn until I got the local server up and running. At this point I deployed to production. Needless to say, there was an error:
Running `gunicorn --workers=4 applications.rocketlistings.wsgi -b 0.0.0.0: -k gevent` attached to terminal... up, run.8052
Traceback (most recent call last):
File "/app/.heroku/python/bin/gunicorn", line 9, in <module>
load_entry_point('gunicorn==0.17.2', 'console_scripts', 'gunicorn')()
File "/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.34-py2.7.egg/pkg_resources.py", line 343, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.34-py2.7.egg/pkg_resources.py", line 2307, in load_entry_point
return ep.load()
File "/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.34-py2.7.egg/pkg_resources.py", line 2013, in load
entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named app.wsgiapp
I'm not sure what I'm doing wrong, and it's very difficult to debug the situation given the obscure remote setup heroku runs. Other threads discussing this error suggest that this is a path problem. This sounds very plausible, but I can't think of what could be wrong with the path, given that I have an init.py file in my applications folder, and everything works locally.
Here is my production path/pythonpath anyway:
Running `echo $PATH` attached to terminal... up, run.2583
/app/.heroku/python/bin:/usr/local/bin:/usr/bin:/bin
Running `echo $PYTHONPATH` attached to terminal... up, run.8026
/app/
Thanks in advance!
Sometimes, this occurs when Gunicorn finds another module called 'app' in the PYTHONPATH. Setting PYTHONPATH to a fake value resolves the issue:
$ heroku config:set PYTHONPATH=fakepath
https://github.com/heroku/heroku-buildpack-python/wiki/Troubleshooting#no-module-named-appwsgiapp
Related
Context
I made a Django react app. Now I want to make it a desktop application so that the user does not have type python manage.py runserver and also activate the environment every time. I used pyinstaller. I did all the steps mentioned for django.
Problem
when I run my executable file made from pyinstaller, I got this error
File "manage.py", line 5, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "djoser\__init__.py", line 6, in <module>
File "importlib\metadata\__init__.py", line 955, in version
File "importlib\metadata\__init__.py", line 928, in distribution
File "importlib\metadata\__init__.py", line 518, in from_name
importlib.metadata.PackageNotFoundError: No package metadata was found for djoser
[2200] Failed to execute script 'manage' due to unhandled exception!
What I have done
I have already installed Djoser in the environment and the environment is also activated. I have also tried to add in manage.py file and also in hidden_import lists but nothing changed. I have also tried adding --copy-meta=djoser in the build command but it got even worse error.
How do I fix this error and If there are any better alternative solutions out there? Thanks
Well I have found for myself another solution . To run this django file by creating a batch file
#echo off
set "VENV_NAME=myenv"
set "PROJECT_DIR=C:\path\to\project"
cd /d %~dp0
cd /d %PROJECT_DIR%
call "%VENV_NAME%\Scripts\activate.bat"
python manage.py runserver
manage.exe runserver --norelaod
I am trying to create a Windows Service which uses a Virtual Python Environment. I create the environment as described here with venv. To create the Windows Service I use winsw.
While I can both run a program in the virtual environment and setup a windows service I cannot figure out how to do both at once.
My first attempt was to call "call win_env/Scripts/activate.bat" in the .bat file which is used by the windows service. This did not work, as he still used the main enviroment and therefore ignored this line of code. Also the second answer in this issue suggests an solution. This also did not work for me as the program still used the main enviroment.
Possible this is because I do not use the env variables in the code but I cannot figure out how I would start the virtual env from there.
When I write that it uses the "main environment" I track this because the program fails because of some missing dependencies (wrong versions). These dependencies are installed in the virtual environment but not in the main environment. If I call the bat files without going into the virtual environment the program fails with the same message.
Error Message:
Exception in thread {X}:
Traceback (most recent call last):
File "{Path}\scoop\apps\anaconda3\current\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "{Path}\scoop\apps\anaconda3\current\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "{Path}\Sources\{Project}\.\populate_cache.py", line 25, in query_failures_from_db
df = pd.read_sql(statement, con, params=[date_from, date_to])
File "{Path}\Sources\{Project}\win_env\lib\site-packages\pandas\io\sql.py", line 563, in read_sql
pandas_sql = pandasSQL_builder(con)
File "{Path}\Sources\{Project}\win_env\lib\site-packages\pandas\io\sql.py", line 744, in pandasSQL_builder
import sqlite3
File "{Path}\scoop\apps\anaconda3\current\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "{Path}\scoop\apps\anaconda3\current\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed while importing _sqlite3: The specified module could not be found.
.bat file:
call uvicorn main:app --port 8590 --host 0.0.0.0
winsw file:
<?xml version="1.0"?>
<!--This configuration file should be placed near the WinSW executable, the name should be the same.E.g. for myapp.exe the configuration file name should be myapp.xmlYou can find more information about configuration options here: https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md -->
<configuration>
<id>Test Programm</id>
<name>Test Programm</name>
<description> XXX. </description>
<executable>{PathToBatfile}</executable>
<onfailure delay="10 sec" action="restart"/>
<onfailure delay="20 sec" action="restart"/>
<onfailure action="none"/>
<resetfailure>1 hour</resetfailure>
<workingdirectory>{PathToWorkingdirectory}</workingdirectory> // .bat file is located here
<priority>Normal</priority>
<stoptimeout>15 sec</stoptimeout>
<stopparentprocessfirst>true</stopparentprocessfirst>
<startmode>Automatic</startmode>
<waithint>15 sec</waithint>
<sleeptime>1 sec</sleeptime>
<logpath>{PathToLog}</logpath>
<!--OPTION: logDefines logging mode for logs produced by the executable.Supported modes:* append - Rust update the existing log* none - Do not save executable logs to the disk* reset - Wipe the log files on startup* roll - Rotate logs based on size* roll-by-time - Rotate logs based on time* rotate - Rotate logs based on size, (8 logs, 10MB each). This mode is deprecated, use "roll"Default mode: appendEach mode has different settings.See https://github.com/kohsuke/winsw/blob/master/doc/loggingAndErrorReporting.md for more details -->
<log mode="roll"> </log>
</configuration>
Based on the input you provided, I think the following batch file should work:
call %~dp0win_env/Scripts/activate.bat
uvicorn main:app --port 8590 --host 0.0.0.0
(The %~dp0 resolves to the directory where the batch file is stored, so it works independent of your current working directory.)
Note that the activate.bat does nothing special. It just updates your PATH environment variable and changes the command prompt, which is convenient during development. However, you could also just call the Python executable from your virtual environment without "activating" it. I'm not familiar with uvicorn, but it seems like it can be called as a Python module as well. In that case you could also use the following batch file:
win_env/Scripts/python.exe -m uvicorn main:app --port 8590 --host 0.0.0.0
yes I know my question sounds like a duplicate but I have tried everything I have found.
I am trying to add a module to my python flask app. This module is located at ../../clients/api relative to my flask app. I haven't had any problems adding a module before, but for some reason this just doesn't want to work.
So far, I have tried:
sys.path.append("mypath") (worked temporarily but as soon as the shell was closed it disappeared)
adding a .pth file in lib/site-packages
adding it directly to my environment variables
adding in in my IDE (Wing 101)
Aside from 1., none of them had any effect whatsoever.
$ flask run
* Serving Flask app "main.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: While importing "main", an ImportError was raised:
Traceback (most recent call last):
File "c:\python37-32\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "my_flask_app_path", line 20, in <module>
from clients.api import Client as client
ModuleNotFoundError: No module named 'api'
Running this on python v-3.7.1, Windows 10, both git bash and cmd. Please try not to be blunt :)
Traceback (most recent call last):
File "./python_runtime/bin/airflow", line 15, in <module>
args.func(args)
File "/home/admin/airflow/python_runtime/lib/python2.7/site-packages/airflow/bin/cli.py", line 423, in webserver
'gunicorn', run_args
File "/home/admin/airflow/python_runtime/lib/python2.7/os.py", line 346, in execvp
_execvpe(file, args)
File "/home/admin/airflow/python_runtime/lib/python2.7/os.py", line 385, in _execvpe
func(fullname, *argrest)
OSError: [Errno 2] No such file or directory
I find out that this error was cause by no gunicorn avaiable,so I print all paths in os.py:
/home/admin/airflow/python_runtime/bin/gunicorn
/home/admin/python2.7.2/bin/gunicorn
/home/admin/tools/local/bin/gunicorn
/home/admin/tools/bin/gunicorn
/home/admin/tools/opsdb/gunicorn
/home/admin/odps_tools/dship/gunicorn
/home/admin/php/bin/gunicorn
/home/admin/cmake/bin/gunicorn
/home/admin/jdk1.6.0_24/bin/gunicorn
/home/admin/tools/bin/gunicorn
/usr/kerberos/bin/gunicorn
/usr/local/sbin/gunicorn
/usr/sbin/gunicorn
/sbin/gunicorn
/usr/local/bin/gunicorn
/bin/gunicorn
/usr/bin/gunicorn
/usr/X11R6/bin/gunicorn
while gunicorn exist in this path
/home/admin/airflow/python_runtime/bin/gunicorn
something wrong with my env?
Airflow seems to expect gunicorn in PATH. For my ubuntu instance this fixed it:
$ export PATH=$PATH:~/.local/bin
$ airflow webserver
On MacOS, it wasn't enough with the proposed solution. If I used relative path (with either ~ or $HOME) it didn't work, but using absolute path solved the issue: PATH="${PATH}:/path/to/.venv/bin"
As you can see, I'm using a hidden folder that I created with Python venv, maybe this is connected.
To test it, is not enough to run gunicorn alone, this worked to me with the relative path as well, but which gunicorn returned nothing, and this indicates that airflow webserver will fail to start.
I have a small rest api built in python using Falcon Framework and it runs in a virtual environment with gunicorn + gevent and pypy 2.3 (I successfully installed the pip packages from gevent#pypy-hacks and gevent-on-pypy/pypycore). I wanted to add a "supervisor" for my application so I first installed supervisord and followed the steps to get it up and running.
The first thing I did in the server was to test the command to start the api; it ran successfully and I could use the api without any issues. The command is:
/bin/bash -c 'cd /path/to/project/api && /path/to/project/env/bin/gunicorn -c settings.py my-api:my_api'
Note: in settings.py I set the parameter daemon=False since I saw that supervisors require that the commands are run in the "foreground" and not daemonized.
Once that was working, I went ahead and created the app's /etc/supervisor/conf.d/my_api.conf conf file with the "command" parameter set as the command above I ran successfully. When I tried to start the api through supervisord, it failed, and in the log of the api, the error was:
Traceback (most recent call last):
File "/path/to/project/env/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
worker.init_process()
File "/path/to/project/env/site-packages/gunicorn/workers/ggevent.py", line 185, in init_process
self.patch()
File "/path/to/project/env/site-packages/gunicorn/workers/ggevent.py", line 74, in patch
_sock=s))
File "/path/to/project/env/site-packages/gevent/socket.py", line 240, in __init__
self.hub = get_hub()
File "/path/to/project/env/site-packages/gevent/hub.py", line 169, in get_hub
hub = _threadlocal.hub = hubtype(*args, **kwargs)
File "/path/to/project/env/site-packages/gevent/hub.py", line 268, in __init__
loop_class = _import(self.loop_class)
File "/path/to/project/env/site-packages/gevent/hub.py", line 198, in _import
return _import(path[-1])
File "/path/to/project/env/site-packages/gevent/hub.py", line 210, in _import
x = __import__(module)
ImportError: No module named gevent.core
So, I was surprised that running the command manually did work, but when supervisor tried to run it, the above error was thrown.
After lots of trial and errors, I decided to install monit to see if it could monitor my api, and I did set it up successfully specifying the "start program" parameter as the aforementioned command.
I was surprised that monit throwed the exact same error. So, does this mean that the nature of my api (pypy + gunicorn + gevent) prevents itself from being monitored by any monitor software around there?
Is there something I'm doing wrong when configuring supervisord/monit?
Any help is greatly appreciated.
Turns out that I have the environment var GEVENT_LOOP already exported in my /etc/environment as follows:
export GEVENT_LOOP=pypycore.loop
Since for running gevent on pypy some hacks are needed including the above export.
But I needed to export it manually in the 'start_program' command entry of Monit (at the end I sticked with Monit), so the resulting command is:
/bin/bash -c 'cd /path/to/project/api && export GEVENT_LOOP=pypycore.loop && /path/to/project/env/bin/gunicorn -c settings.py my-api:my_api'