Deploying python 2.7 flask app

Hello, I have no choice but to use python 2.7. However i am having trouble deploying my flask application.
python: No module named venv
python: No module named venv
venv doesnt exist until python 3 , how can i deploy it with virtualenv instead?
15 Replies
Percy
Percy2y ago
Project ID: N/A
Percy
Percy2y ago
⚠️ experimental feature
jackson
jackson2y ago
wouldn't recommend using a virtual environment in a production setting, the containers are all destroyed and remade on each redeploy anyways so you can just have a requirements.txt file in the root directory and it will be automatically picked up and installed if you're using nixpacks
jakeAnon
jakeAnonOP2y ago
is there way to disable? my requirements.txt file just has
Flask==1.1.2
flask_cors==3.0.10
python-dotenv==0.18.0
Flask==1.1.2
flask_cors==3.0.10
python-dotenv==0.18.0
started failing once i added a runtime.txt with python-2.7.10
jackson
jackson2y ago
try removing the last line there, but you'll need to set the nixpacks python version like documented here https://nixpacks.com/docs/providers/python
Python | Nixpacks
App source + Nix packages + Docker = Image
jackson
jackson2y ago
and rename any Dockerfile you may have to something like Dockerfile.dev
jakeAnon
jakeAnonOP2y ago
thanks, ill give it a try and report back For people in future: Was able to get it the venv addressed by using following nixpacks.toml
[phases.setup]
nixPkgs = ['python27', 'gcc', 'virtualenv']

[phases.install]
cmds = ['virtualenv /opt/virtualenv && . /opt/virtualenv/bin/activate && pip install -r requirements.txt']

[start]
cmd = 'python main.py'
[phases.setup]
nixPkgs = ['python27', 'gcc', 'virtualenv']

[phases.install]
cmds = ['virtualenv /opt/virtualenv && . /opt/virtualenv/bin/activate && pip install -r requirements.txt']

[start]
cmd = 'python main.py'
However I am now getting new error, Traceback (most recent call last): File "main.py", line 1, in <module> from flask import Flask, request, abort ImportError: No module named flask with my requirements.txt
virtualenv==16.7.10
Flask==1.1.2
flask_cors==3.0.10
python-dotenv==0.18.0
virtualenv==16.7.10
Flask==1.1.2
flask_cors==3.0.10
python-dotenv==0.18.0
jackson
jackson2y ago
why is flask in there twice? should just be the capital f Flask iirc
jakeAnon
jakeAnonOP2y ago
my apologies it is just the capital F, meant to delete that
jackson
jackson2y ago
ah so the start command doesn't activate the virtualenv first change it to be like this
[phases.setup]
nixPkgs = ['python27', 'gcc', 'virtualenv']

[phases.install]
cmds = ['virtualenv /opt/virtualenv && . /opt/virtualenv/bin/activate && pip install -r requirements.txt']

[start]
cmd = ['virtualenv /opt/virtualenv && . /opt/virtualenv/bin/activate && python main.py']
[phases.setup]
nixPkgs = ['python27', 'gcc', 'virtualenv']

[phases.install]
cmds = ['virtualenv /opt/virtualenv && . /opt/virtualenv/bin/activate && pip install -r requirements.txt']

[start]
cmd = ['virtualenv /opt/virtualenv && . /opt/virtualenv/bin/activate && python main.py']
jakeAnon
jakeAnonOP2y ago
I changed the [start] cmd to a single string as thats what it was expecting
created virtual environment CPython3.10.9.final.0-64 in 423ms
creator CPython3Posix(dest=/opt/virtualenv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: Flask==1.1.2, Flask_Cors==3.0.10, Jinja2==3.1.2, MarkupSafe==2.1.2, Werkzeug==2.2.3, click==8.1.3, itsdangerous==2.1.2, pip==22.2.2, python_dotenv==0.18.0, setuptools==65.3.0, six==1.16.0, virtualenv==16.7.10, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
Traceback (most recent call last):
File "/app/main.py", line 1, in <module>
from flask import Flask, request, abort
File "/opt/virtualenv/lib/python3.10/site-packages/flask/__init__.py", line 14, in <module>
from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/opt/virtualenv/lib/python3.10/site-packages/jinja2/__init__.py)
created virtual environment CPython3.10.9.final.0-64 in 423ms
creator CPython3Posix(dest=/opt/virtualenv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: Flask==1.1.2, Flask_Cors==3.0.10, Jinja2==3.1.2, MarkupSafe==2.1.2, Werkzeug==2.2.3, click==8.1.3, itsdangerous==2.1.2, pip==22.2.2, python_dotenv==0.18.0, setuptools==65.3.0, six==1.16.0, virtualenv==16.7.10, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
Traceback (most recent call last):
File "/app/main.py", line 1, in <module>
from flask import Flask, request, abort
File "/opt/virtualenv/lib/python3.10/site-packages/flask/__init__.py", line 14, in <module>
from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/opt/virtualenv/lib/python3.10/site-packages/jinja2/__init__.py)
looks like the virtualenv its creating is using 3.10.9 and not 2.7
jackson
jackson2y ago
try removing virtualenv entirely and just using something like this
[phases.setup]
nixPkgs = ['python27', 'gcc', 'virtualenv']

[phases.install]
cmds = '/usr/bin/python2 -m pip install -r requirements.txt'

[start]
cmd = '/usr/bin/python2 main.py'
[phases.setup]
nixPkgs = ['python27', 'gcc', 'virtualenv']

[phases.install]
cmds = '/usr/bin/python2 -m pip install -r requirements.txt'

[start]
cmd = '/usr/bin/python2 main.py'
i think that would be where nix installs the python2 package
jakeAnon
jakeAnonOP2y ago
/usr/bin/python2: No such file or directory
&
/usr/bin/python2.7: No such file or directory
&
/usr/bin/python27: No such file or directory
/usr/bin/python2: No such file or directory
&
/usr/bin/python2.7: No such file or directory
&
/usr/bin/python27: No such file or directory
odd because that one link you shared has 2.7 on there
Brody
Brody2y ago
could have updated your app to use python3 by now
jakeAnon
jakeAnonOP2y ago
trust me if i could i would
Want results from more Discord servers?
Add your server