Could not run Ec2-server on port 80 - python

I could run the server on port 8000 but when i try to use 80 with
python manage.py runserver myip:80 I get:
You don't have permission to access that port.
If I use sudo python manage.py runserver myip:80 I get:
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
If I write python in the console I get version 3.5.5 and my env is activated.
EDIT:
Using sudo python3 manage.py runserver myip:80 I get:
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'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

You are getting this because you aren't using Python 3. The easiest way to fix this is to create a virtualenv that uses python3 as it's python executable.
sudo pip3 install virtualenv
virtualenv -p python3 envname
workon envname
pip install django
pip install your_other_dependencies
Still, as others have said, running the Django webserver in a production environment is dicey at best, and spending some time setting up a Gunicorn/Nginx (or appropriate substitute) will pay dividends long term.

There are a couple of things going on here. First of all, only privileged users (e.g. root or other users via sudo) can bind to ports under 1024.
But more importantly, manage.py runserver should never be used in production:
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)
I strongly advise you to set up a proper web server instead. If you search for "EC2 Django" you'll find plenty of walkthroughs on how to do this properly.

Related

SyntaxError: invalid syntax to repo init in the AOSP code

I have tried to repo init the source code Ubuntu build machine and it is successfully able to clone the code.
repo init -u git#github.com:xxx/xx_manifest.git -b xxx
Now I am trying repo init the source code in VM Ubuntu machine.
In between getting the error like below:
Traceback (most recent call last):
File "/xxx/.repo/repo/main.py", line 56, in <module>
from subcmds.version import Version
File "/xxx/.repo/repo/subcmds/__init__.py", line 38, in <module>
['%s' % name])
File "/xxx/.repo/repo/subcmds/upload.py", line 27, in <module>
from hooks import RepoHook
File "/xxx/.repo/repo/hooks.py", line 472
file=sys.stderr)
^
SyntaxError: invalid syntax
python version is same in build machine and vm machine 2.7.17.
try these commands
curl https://storage.googleapis.com/git-repo-downloads/repo-1 > ~/bin/repo
chmod a+x ~/bin/repo
python3 ~/bin/repo init -u git#....
I just had the same issue and this resolved it for me :
Download last version of repo :
curl https://storage.googleapis.com/git-repo-downloads/repo-1 > repo
Change right to make it executable : chmod a+x repo
Run your repo init with python3 and the "repo" you just download : python3 repo init -u git#github.com:xxx/xx_manifest.git -b xxx
I have experienced the same issue on Ubuntu 18.04 during the installation of the OpenSTLinux Yocto layer with the following command:
repo init -u https://github.com/STMicroelectronics/oe-manifest.git -b refs/tags/openstlinux-5.4-dunfell-mp1-20-11-12
Returns:
Get https://gerrit.googlesource.com/git-repo/clone.bundle
Get https://gerrit.googlesource.com/git-repo
remote: Counting objects: 2, done
remote: Finding sources: 100% (117/117)
remote: Total 117 (delta 63), reused 117 (delta 63)
Receiving objects: 100% (117/117), 142.25 KiB | 11.85 MiB/s, done.
Resolving deltas: 100% (63/63), completed with 32 local objects.
From https://gerrit.googlesource.com/git-repo
1469c28..0588f3d main -> origin/main
* [new tag] v2.11 -> v2.11
* [new tag] v2.11.1 -> v2.11.1
Traceback (most recent call last):
File "/home/xxx/Distribution-Package/openstlinux-5.4-dunfell-mp1-20-11-12/.repo/repo/main.py", line 56, in <module>
from subcmds.version import Version
File "/home/xxx/Distribution-Package/openstlinux-5.4-dunfell-mp1-20-11-12/.repo/repo/subcmds/__init__.py", line 38, in <module>
['%s' % name])
File "/home/xxx/Distribution-Package/openstlinux-5.4-dunfell-mp1-20-11-12/.repo/repo/subcmds/upload.py", line 27, in <module>
from hooks import RepoHook
File "/home/xxx/Distribution-Package/openstlinux-5.4-dunfell-mp1-20-11-12/.repo/repo/hooks.py", line 472
file=sys.stderr)
This issue goes away with using Python3 instead of Python (2.7). You can do this:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python
Try below command to make it will work 100%, tried & suggested
mkdir -p ~/.bin
PATH="${HOME}/.bin:${PATH}"
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo
chmod a+rx ~/.bin/repo
One solution is to modify the first line of /usr/bin/repo and change it from
#!/usr/bin/python
to
#!/usr/bin/python3
This asks the system to use Python3 instead of the default Python.
If the system you are running on doesn't have python3, like in my case, and you don't have the option to install python3, or installing it breaks other parts, the option is the degrade repo to a version that uses python2.7:
- git clone https://gerrit.googlesource.com/git-repo
- cd git-repo
- git reset --hard v1.13.11
- mkdir -p ~/.bin
- PATH="${HOME}/.bin:${PATH}"
- cp repo ~/.bin/repo
- chmod a+rx ~/.bin/repo
This will use v1.13.11 of repo, which works with python2.7
As seen in a similar error in arvestad/alv issue 1, that would be consistent with running the process with Python 2.7 instead of Python3
Double-check you Python version between:
your Ubuntu build machine (where the repo init works)
your VM Ubuntu machine (where the repo init fails)
Same error here, with the error suggesting you are executing python2 with a PYTHONPATH that's only appropriate for python3.
I don't know exactly how this works, but i just had the same issue and this resolved it for me it seems.
https://source.android.com/setup/develop#installing-repo
Dont use the legacy one, use the first one to resolve it.
Edit: It seems you also need to have python 3.6 installed on you system to have this work. You can still have update-alternatives point to python 2.7, you simply need 3.6 or newer installed.
Just install python3 and the latest repo.
I encountered the problem too, but on Mac OS. The log is exactly the same as yours.
Definitely python2 caused this problem. repo is try to run python3 files in python2 environment.
I found the this from repo docs https://gerrit.googlesource.com/git-repo/+/refs/heads/master/docs/python-support.md
So I update my repo (which located in depot_tools). Since I already have python3 installed, everything is ok now.
Hope my experience may help you.

Virtual environment is not working in Django

I am deploying my Django app on digitalocean but I get an error after the giving the command in my terminal to migrate. Help me to figure out the error so that I can fix.
terminal output
urban#ubuntu-s-1vcpu-1gb-blr1-01:~$ source bin/activate
(urban) urban#ubuntu-s-1vcpu-1gb-blr1-01:~$ cd lok
(urban) urban#ubuntu-s-1vcpu-1gb-blr1-01:~/lok$ python manage.py migrate
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
(urban) urban#ubuntu-s-1vcpu-1gb-blr1-01:~/lok$ ls
business List manage.py news sports
templates
db.sqlite3 lokswar movie README.md static_my_project
Your version of Django requires Python 3, but it looks like python is python2, even after activating the virtual environment. You can use which python to check that python is pointing to the executable in your virtual environment.
If your virtual environment was created with python 2, then you'll have to create a new virtual environment that uses python 3.
deactivate
python3 -m venv /path/to/urban.new
source /path/to/urban.new/bin/activate
# install requirements

Luigi Pipelining : No module named pwd in Windows

I am trying to execute the tutorial given in https://marcobonzanini.com/2015/10/24/building-data-pipelines-with-python-and-luigi/.
I am able to run the program on its own using local scheduler, giving me:
Scheduled 2 tasks of which:
* 2 ran successfully:
- 1 PrintNumbers(n=1000)
- 1 SquaredNumbers(n=1000)
This progress looks :) because there were no failed tasks or missing external de
pendencies
===== Luigi Execution Summary =====
However, to try the visualization on the server, when I try to run luigid --background, it throws me an error saying I dont have pwd module.
I cannot find a pwd module using pip for windows.
File "c:\users\alex\appdata\local\continuum\anaconda3\lib\site-packages
\luigi\process.py", line 79, in daemonize
import daemon
File "c:\users\alex\appdata\local\continuum\anaconda3\lib\site-packages
\daemon\__init__.py", line 42, in <module>
from .daemon import DaemonContext
File "c:\users\alex\appdata\local\continuum\anaconda3\lib\site-packages
\daemon\daemon.py", line 25, in <module>
import pwd
ModuleNotFoundError: No module named 'pwd'
I am working in Anaconda Spyder with Python 3.6
I was able to fix this by installing python-daemon==2.1.2
If you already have python-daemon, try downgrading to version 2.1.2
Do this before install luigi.
Example:
pip install python-daemon==2.1.2
then
pip install luigi.
For some reason, if you dont use the --background parameter on windows it will start just fine
just write luigid in cmd
The base problem here is that luigid --background is trying to spawn a python-daemon which is a unix-specific thing.
See section titled "The luigid server" here: http://luigi.readthedocs.io/en/stable/central_scheduler.html
Specifically:
Note that this requires python-daemon. By default, the server starts on AF_INET and AF_INET6 port 8082 (which can be changed with the --port flag) and listens on all IPs. (To use an AF_UNIX socket use the --unix-socket flag)
This existing stack overflow answer provides more detail:
How to start daemon process from python on windows?
Options I see here are:
Log a request with Luigi on github to improve their windows support to spawn Luigid as a windows process for the --background switch
Run a virtual machine with a proper Unix OS in it on Windows and run your Luigi pipelines there.
Follow Steven G's suggestion and run luigid in a separate command prompt
To reproduce the root cause of this issue, open a python prompt in windows and type:
>>import daemon
Traceback (most recent call last): File "", line 1, in
File "C:\Anaconda3\lib\site-packages\daemon__init__.py",
line 42, in
from .daemon import DaemonContext File "C:\Anaconda3\lib\site-packages\daemon\daemon.py", line 25, in
import pwd ModuleNotFoundError: No module named 'pwd'

Django manage.py runserver invalid syntax

I am developing a web in Ubuntu using django. Everything works normal. Now, I want to change my computer which use Windows. When I try to runserver, it gives:
E:\DEGNet>py manage.py runserver
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
E:\DEGNet>py
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
As shown above, I have installed Python 3.6.3. I have installed django and other necessary library using pip3 too.
Edit: manage.py file, it is a default manage.py that I get when generating the project.
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DEGNet.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
I faced same problem but now solved with this cmd:
python3 manage.py runserver
Edit your manage.py file as given below:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DEGNet.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
execute_from_command_line(sys.argv)
Note that from exc is removed from the file. It is not required in the manage.py file.
Make sure that your virtualenv is activated. Suppose the name of your virtualenv is pythonpy, then run these commands:
virtualenv pythonpy
workon pythonpy #After running these command, you should see something like this but your file path may be different: "(pythonpy) C:\Users\ MyDjangoProject \
Then go to the project folder which contains manage.py
(pythonpy) C:\Users\ MyDjangoProject \ #Same path as above
Then simple run the server:
python manage.py runserver #This will give you the project path to the localhost. Copy and paste the URL in the browser and should work.
Try (from command line):
python3 manage.py runserver
If I used this (no python3) instead:
python manage.py runserver
the error persisted. This method allows you to not have to alter manage.py (so you can keep "from exc").
You just forgot to activate the virtual environment , do it like that :
source /home/adel/Dev/Python/DjangoProjects/myproject/venv/bin/activate
And than you can run the server :
python manage.py runserver
i've also got this error but solve it by installing pipenv first,
try run this first
pipenv shell django==2.1
then you should be able to run
python3 manage.py runserver
Ensure you're running the app from virtualenv i.e if you've created a virtualenv for your project, then activate the venv first.
me#debian:~/Desktop/webapp$source venv/bin/activate
(venv) me#debian:~/Desktop/webapp$python manage.py runserver
What's happening is that the wrong version of python is being used, which may not have all the dependencies in your virtualenv. I get this error when using sudo manage.py: using sudo changes the version of python which is being used to /usr/bin/python.
The problem is solved by specifying which version of python to use when using sudo:
sudo /path/to/my/env/bin/python manage.py makemigrations
I have met the same problem, and I find it strange because I have activated the virtualenvironment and set the python3, however, I meet this problem when I use this statement "python manage.py runserver".I use this statement often but I meet this only once, and I restart my virtualenvironment and it runs, hope you wil,too.
I was facing the same issue.
Activated venv & ran python manage.py runserver wasn't working.
I could see the venv was activated but still it wasn't working. Then tried python3 manage.py runserver which got rid of exc issue but now it wasnt detecting my installed libraries.
The change I made that broke it was renaming the base folder ( kind of same situation like OP base folder location was different now).
But wait, how does this impact anything?
A variable that stores a full path to venv inside venv/bin/activate is
now no longer valid.
Resolution:
deactivate venv if already running
open venv/bin/activate
search for VIRTUAL_ENV variable and rename as per the new folder changes made.
activate venv source venv/bin/activate
run python manage.py runserver
Tada, everything is back to normal and we can happily enjoy our lemonade.
I've got also the same issue with Python 3.4.4 and Django 2.0. I tried the last solution, nothing works (no need to delete that: from exc on the line 14).
Just run your server with:
python manage.py runserver
Instead of:
./manage.py runserver #or '.\manage.py runserver' for Windows
I have no problem running this way:
sudo ./**(your path)**/bin/python manage.py runserver
i had the same problem. I solved it by simply specifying the python version i.e type python3 manage.py runserver instead of python manage.py runserver
i re installed the v env
virtualenv venv --python=python3.7
i insttalled django
it worked
I had come out of my virtial environment.
I re-ran pipenv shell
Do this first on your command line where your Env folder is found:
source Env/bin/activate
Then now navigate to your project directory and do:
python manage.py runserver
It works!!
Just use python3 manage.py runserver instead python manage.py runserver

How do I run a Python script that is part of an application I uploaded in an AWS SSH session?

I'm trying to run a Python script I've uploaded as part of my AWS Elastic Beanstalk application from my development machine, but can't figure out how to. I believe I've located the script correctly, but when I attempt to run it under SSH, I get an import error.
For example, I have a Flask-Migrate migration script as part of my application (pretty much the same as the example in the documentation), but after successfully SSHing to my EB instance with
> eb ssh
and locating the script with
$ sudo find / -name migrate.py
when I run in the directory (/opt/python/current) where I located it with
$ python migrate.py db upgrade
at the SSH prompt I get
Traceback (most recent call last):
File "db_migrate.py", line 15, in <module>
from flask.ext.script import Manager
ImportError: No module named flask.ext.script
even though my requirements.txt (present along with the rest of my files in the same directory) has flask-script==2.0.5.
On Heroku I can accomplish all of this in two steps with
> heroku run bash
$ python migrate.py db upgrade
Is there equivalent functionality on AWS? How do I run a Python script that is part of an application I uploaded in an AWS SSH session? Perhaps I'm missing a step to set up the environment in which the code runs?
To migrate your database the best is to use container_commands, they are commands that will run every time you deploy your application. There is a good example in the EBS documentation (Step 6) :
container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
The reason why you're getting an ImportError is because EBS installs your packages in a virtualenv. Before running arbitrary scripts in your application in SSH, first change to the directory containing your (latest) code with
cd /opt/python/current
and then activate the virtualenv
source /opt/python/run/venv/bin/activate
and set the environment variables (that your script probably expects)
source /opt/python/current/env

Categories

Resources