Python Flask not working properly on Bash on Ubuntu on Windows - python

I am trying to run a flask app on my Windows 10 laptop through Bash on Ubunutu on Windows, but I am getting the following error:
None
Traceback (most recent call last):
File "app.py", line 126, in
app.run(debug=True) #Deubug is set to true
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 841, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 720, in run_simple
s.bind((hostname, port))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 13] Permission denied
I am not trying to run a restricted port. It should be on port 5000.
Here are the commands I ran:
sudo apt-get install python-pip
sudo pip install Flask
sudo pip install flask-bootstrap
sudo pip install bokeh
python app.py
Whenever Flask was installed, I got back a warning:
The directory '/home/User/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/User/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Also, running sudo python app.py does not help either.
Any thoughts on what is happening?

Related

Cannot install private dependency from artifact registry inside docker build when pulling from Github

I am trying to deploy a Cloud run application containing a private python package.
The code from the cloudrun is hosted on Github, and when I push code, it triggers a cloudbuild that builds the Docker, pushes it to the Container registry and creates a cloudrun with the image.
Unfortunately, in the docker build stage. The Docker cannot access the private python package that is available on the artifact registry.
I have sucessfully used that package in a cloud function in the past, so I am sure the package works. I have also given the same permissions to the cloudbuild that builds the docker to cloudbuilds that buils functions using that package, and they work.
I have created this issue in the past here, and had possible solutions using the Json Key file of a service account with the owner permission on the project following that tutorial from the Google Cloud documentation. But I would like to avoid using a key, as the key should not be saved on Github. I am sure this is a permission issue, but I could not figure it out.
cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-t', 'gcr.io/${_PROJECT}/${_SERVICE_NAME}:$SHORT_SHA', '--network=cloudbuild', '.', '--progress=plain']
Dockerfile
FROM python:3.8.6-slim-buster
ENV APP_PATH=/usr/src/app
ENV PORT=8080
# Copy requirements.txt to the docker image and install packages
RUN apt-get update && apt-get install -y cython
RUN pip install --upgrade pip
# Set the WORKDIR to be the folder
RUN mkdir -p $APP_PATH
COPY / $APP_PATH
WORKDIR $APP_PATH
RUN pip install -r requirements.txt --no-color
RUN pip install --extra-index-url https://us-west1-python.pkg.dev/my-project/my-package/simple/ my-package==0.2.3 # This line is where the bug occurs
# Expose port
EXPOSE $PORT
# Use gunicorn as the entrypoint
CMD exec gunicorn --bind 0.0.0.0:8080 app:app
The permissions I added are:
cloudbuild default service account (project-number#cloudbuild.gserviceaccount.com): Artifact Registry Reader
service account running the cloudbuild : Artifact Registry Reader
service account running the app: Artifact Registry Reader
The cloudbuild error:
Step 10/12 : RUN pip install --extra-index-url https://us-west1-python.pkg.dev/my-project/my-package/simple/ my-package==0.2.3
---> Running in b2ead00ccdf4
Looking in indexes: https://pypi.org/simple, https://us-west1-python.pkg.dev/muse-speech-devops/gcp-utils/simple/
User for us-west1-python.pkg.dev: [91mERROR: Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 167, in exc_logging_wrapper
status = run_func(*args)
File "/usr/local/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 205, in wrapper
return func(self, options, args)
File "/usr/local/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 340, in run
requirement_set = resolver.resolve(
File "/usr/local/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 94, in resolve
result = self._result = resolver.resolve(
File "/usr/local/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 481, in resolve
state = resolution.resolve(requirements, max_rounds=max_rounds)
File "/usr/local/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 348, in resolve
self._add_to_criteria(self.state.criteria, r, parent=None)
File "/usr/local/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 172, in _add_to_criteria
if not criterion.candidates:
File "/usr/local/lib/python3.8/site-packages/pip/_vendor/resolvelib/structs.py", line 151, in __bool__

How do I install mod_wsgi on Mac (High Sierra)?

I'm using Mac 10.13.6 (High Sierra) with this version of Apache ...
localhost:mod_wsgi-4.6.5 davea$ httpd -version
Server version: Apache/2.4.33 (Unix)
Server built: Apr 3 2018 17:54:07
How do I install the mod_wsgi module so that Apache can load my Python site? I tried using pip but got the most baffling permission errors despite the fact I'm using sudo ...
(venv) localhost:tmp davea$ sudo pip install mod_wsgi
The directory '/Users/davea/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/davea/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: mod_wsgi in /Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages (4.6.5)
You are using pip version 19.0.1, however version 19.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv) localhost:tmp davea$
(venv) localhost:tmp davea$ sudo mod_wsgi-express install-module
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/mainpage_project/venv/bin/mod_wsgi-express", line 11, in <module>
load_entry_point('mod-wsgi==4.6.5', 'console_scripts', 'mod_wsgi-express')()
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/mod_wsgi/server/__init__.py", line 3570, in main
cmd_install_module(args)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/mod_wsgi/server/__init__.py", line 3508, in cmd_install_module
shutil.copyfile(where(), target)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 121, in copyfile
with open(dst, 'wb') as fdst:
PermissionError: [Errno 1] Operation not permitted: '/usr/libexec/apache2/mod_wsgi-py37.cpython-37m-darwin.so'
The instructions outlined under the link below, work under OSX 10.14 (Mojave):
https://modwsgi.readthedocs.io/en/develop/user-guides/installation-on-macosx.html
Based on your output, it seems you are hitting some permission issues.

Ansible commands work when root, but not when default user. Throws a python PyYAML error only as default user, but then works for root

I recently ran some PIP commands in my server, and after doing so none of the Ansible commands would work as the default user. If I try to run any Ansible commands as the default user, I get the following error -
Traceback (most recent call last):
File "/usr/bin/ansible-playbook", line 4, in <module>
from pkg_resources import require; require('ansible==2.2.0')
File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 2659, in <module>
parse_requirements(__requires__), Environment()
File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 546, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: PyYAML
However, if I sudo and then try to run any ansible commands, it seems to work fine. I think I might have messed something up with the python packages that were installed with PIP, and now they only work for root.
How can I get the default user to be able to run these commands again?
Fixed it. The issue was with permissions when I ran the pip install. Running
chmod -R ugo+rX /usr/lib64/python2.6/site-packages/ and
chmod -R ugo+rX /usr/lib/python2.6/site-packages/ fixed the issue.

bad interpreter while attempting pip install flask

I do have Flask installed at my root directory, however whenever I change directories and run a virutalenv venv to attempt to install flask via pip install flask I receive the following error
bash: /home/phillipsk/rampup/sql/venv/bin/easy_install: /home/phillipsk/rampup/webapp/sql/venv/bin/python: bad interpreter: No such file or directory
Here are the contents of the venv directory
activate activate.csh activate.fish activate_this.py easy_install easy_install-2.7 pip pip2 pip2.7 python python2 python2.7
I can run /home/phillipsk/rampup/webapp/sql/venv/bin/python
and the python interpreter opens
Python 2.7.6
here is the ls -l of
/home/phillipsk/rampup/webapp/sql/venv/bin/python
-rwxr-xr-x 1 root root 3349512 Aug 15 13:11 /home/phillipsk/rampup/webapp/sql/venv/bin/python
This is my attempt at running pip install flask
> (venv)phillipsk#phillips:~/rampup/webapp/sql/venv/bin$ pip install flask Downloading/unpacking flask Cleaning up... Exception: Traceback
> (most recent call last): File
> "/home/phillipsk/rampup/webapp/sql/venv/local/lib/python2.7/site-packages/pip/basecommand.py",
> line 122, in main
> status = self.run(options, args) File "/home/phillipsk/rampup/webapp/sql/venv/local/lib/python2.7/site-packages/pip/commands/install.py",
> line 278, in run
> requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle) File
> "/home/phillipsk/rampup/webapp/sql/venv/local/lib/python2.7/site-packages/pip/req.py",
> line 1153, in prepare_files
> location = req_to_install.build_location(self.build_dir, not self.is_download) File
> "/home/phillipsk/rampup/webapp/sql/venv/local/lib/python2.7/site-packages/pip/req.py",
> line 218, in build_location
> _make_build_dir(build_dir) File "/home/phillipsk/rampup/webapp/sql/venv/local/lib/python2.7/site-packages/pip/req.py",
> line 1527, in _make_build_dir
> os.makedirs(build_dir) File "/home/phillipsk/rampup/webapp/sql/venv/lib/python2.7/os.py", line
> 157, in makedirs
> mkdir(name, mode) OSError: [Errno 13] Permission denied: '/home/phillipsk/rampup/webapp/sql/venv/build'
>
> Storing debug log for failure in /home/phillipsk/.pip/pip.log
sudo pip install flask does work but regardless after running either pip commands I immediately enter into a python interpreter and import flask with no success
As a temporary solution, try changing ownership of your venv folder and all underlying files and folders:
sudo chown -R yourusername:yourusersgroup venv
then reactivate virtualenv and try instaling flask without sudo.
Btw. if you are using sudo when creating virtualenvs, you shouldn't. Run virtualenv /path/to/virtualenv without sudo and logged in as a regular user, activate it and then pip install stuff. Hope this helps.
As a sidenote look into virtualenvwrapper project. It might make things easier for you in the future.
http://virtualenvwrapper.readthedocs.org/en/latest/
the directory is owned by root,
you said it yourself
-rwxr-xr-x 1 root root 3349512 Aug 15 13:11 /home/phillipsk/rampup/webapp/sql/venv/bin/python
so to do it just
sudo pip install flask

linking python project with virtualenv

I have an ubuntu server 12 running as a guest in virtualbox on a windows 7 host.
In a shared folder /workspace I have a python project project01. Since virtualbox has issues with creating symlinks in a shared folder I created the virtualenv in my users home directory. /home/user1/venv.
I can activate the virtual environment no problem.
source ~/venv/bin/activate
but when I try to run manage.py, I get an error
~/venv/bin/python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
I'm sure I'm missing a path or something, but I'm not sure exactly what path to add and to where. I tried setting PYTHONPATH in the activate script, but that didn't work. Do I need to add the path to django's core management in the activate script?
I see a bunch of django directories under venv/build
Django
django-appconf
django-celery
django-celery-email
django-celery-transactions
django-debug-toolbar
django-filebrowser
django-grappelli
djangohelpers
django-imagekit
django-pipeline
django-redis
django-sslify
django-storages
django-widget-tweaks
do I need to point in there somewhere?
I'm very much a django and virtualenv noob.
UPDATE
It doesn't look like the packages were installed. So this is another matter entirely.
Thanks for your reply. I have some other issues I need to figure out here.
I was in the virtualenv and ran pip install -r requirements.txt which only had 1 error for postgres, but I'm not using a local postgres server so that's not a huge deal. Unless that error is preventing everything from installing, then it is a huge deal.
when I check site-packages I only see the following:
easy-install.pth
pip-1.2.1-py2.7.egg
setuptools-0.6c11-py2.7.egg
setuptools.pth
and pip freeze shows me:
argparse==1.2.1
wsgiref==0.1.2
the error from pip install is:
Exception information:
Traceback (most recent call last):
File "venv/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/basecommand.py", line 107, in main
status = self.run(options, args)
File "venv/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/commands/install.py", line 256, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "venv/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/req.py", line 1042, in prepare_files
req_to_install.run_egg_info()
File "venv/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/req.py", line 236, in run_egg_info
command_desc='python setup.py egg_info')
File "venv/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/util.py", line 612, in call_subprocess
% (command_desc, proc.returncode, cwd))
InstallationError: Command python setup.py egg_info failed with error code 1 in venv/build/psycopg2
The error looks as if you do not have Django in the venv python path.
You can confirm that it is part of your venv python install using a couple of ways:
If you used PIP to install you can verify that the package is installed using pip freeze
Also check in lib/python2.7/site-packages/ of the venv there should be a django directory in there. This is the directory that is part of the PYTHONPATH for your venv.
If you cannot find the modules in there of have installed them in a different path. Then you will need to make sure that the path is part of the PYTHONPATH of the venv.

Categories