How to install django with python3.9 on ubuntu aws - django

I'm having challenges installing django using the command sudo python3.9 -m pip install Django. The error I get running that command is:
Traceback (most recent call last):
File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/lib/python3/dist-packages/pip/__main__.py", line 19, in <module>
sys.exit(pip.main())
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 217, in main
return command.main(cmd_args)
File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 242, in main
with self._build_session(
File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 66, in _build_session
session = PipSession(
File "/usr/lib/python3/dist-packages/pip/download.py", line 321, in __init__
self.headers["User-Agent"] = user_agent()
File "/usr/lib/python3/dist-packages/pip/download.py", line 93, in user_agent
zip(["name", "version", "id"], platform.linux_distribution()),
AttributeError: module 'platform' has no attribute 'linux_distribution'
It's been a hard time trying to run sudo python manage.py migrate. From the comment/answer from this Stack Overflow post Traceback while running 'sudo python3 manage.py migrate' I got to know that I need python 3.6 and above. When I created the aws ubuntu server instance I ran python3 -V and the ouput was that python3.5 was running on the machine. I upgraded it to python 3.9. Now when I run python3 -V the output is: Python 3.9.4. After upgrading to python3.9 I created and activated another virtual enviroment.
Now when I try to install django using the command sudo python3.9 -m pip install Django I get the above error. If I use sudo python3 -m pip install Django the django is installed with python3.5 because, thereafter when I run sudo python3 manage.py migrate it throws back an error pointing to File "/usr/lib/python3.5/runpy.py", line 197, in _run_module_as_main If I delete python 3.5 in /usr/lib folder (since I still have python 3.9 there), python3.5 is recreated in the /usr/lib folder whenever I reinstall django using sudo python3 -m pip install Django (even if python3 -V is outputting Python 3.9.4). When I run sudo python3 manage.py migrate it says I don't have django installed in my enviroment variable.
Now the explanation is getting boring. PLEASE HOW DO I INSTALL DJANGO ON UBUNTU AWS USING PYTHON3.9?

sudo apt-get install python3-setuptools
sudo apt remove python3-pip
sudo python3.9 -m easy_install pip
then try to install django again
python3 -m pip install Django
Don't forget to create a virtual environment and specify the python version first

I later found out that it is not possible to run sudo python3.9 -m pip install Django. As stated in the stack overflow post found in the link Traceback while running 'sudo python3 manage.py migrate', python3.5 does not support it.
I had to install python3.6. I was able to use this python version to install django by runnig sudo python3.6 -m pip install Django.
I'm definitely not satisfied with these sorts of breaking changes that exist with python releases, but I'm glad I got the program running.

Related

Why do I get syntax error when I try to do a pip install on CentOS 6?

I'm trying to install a python module called cymysql on a CentOS 6 system by commands like
yum install -y python-setuptools && easy_install pip && pip install cymysql
However I received an Error message saying there're a few lines got SyntaxError: invalid syntax ( see the error code as following)---It actually happened when I run the third command pip install cymysql
Traceback (most recent call last):
File "/usr/local/bin/pip", line 11, in <module>
load_entry_point('pip==21.0', 'console_scripts', 'pip')()
File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 561, in load_entry_point
File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2631, in load_entry_point
File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2291, in load
File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2297, in resolve
File "/usr/local/lib/python2.7/site-packages/pip-21.0-py2.7.egg/pip/_internal/cli/main.py", line 60
sys.stderr.write(f"ERROR: {exc}")
^
SyntaxError: invalid syntax
Not so sure what's happening here, just wondering if anyone can help? (I enclosed the pip file and main.py here but I don't know where is the init.py file).BTW, It's centOS 6 but I have installed the Python 3.6.10 on the server.When I run the command python3.6 -V, I got
python3.6.10
FIY
PIP file Code:
#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==21.0','console_scripts','pip'
__requires__ = 'pip==21.0'
import re
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(
load_entry_point('pip==21.0', 'console_scripts', 'pip')() //**THIS IS LINE 11**
)
Line 57 to 62 in Main.py
try:
cmd_name, cmd_args = parse_command(args)
except PipError as exc:
sys.stderr.write(f"ERROR: {exc}") //**THIS IS LINE 60**
sys.stderr.write(os.linesep)
sys.exit(1)
&tldr;
On an RPM-based distro, you use RPM packages as much as possible.
Never run pip as root user
You must not use pip as root user (or asking for trouble).
There is a whole infrastructure of Python packages installable through yum and this is what you should use.
You can still use pip to install Python modules, but only in very rare cases when a Python module is not available as a package. In that case you must use pip inside a virtual Python environment. Your case is such.
Make sure you have fixed yum for base, EPEL and set up SCLO repositories, since CentOS 6 is EOL.
Then:
# Ensure EPEL repository
yum -y install epel-release
# fix EPEL repo due to EOL:
curl https://www.getpagespeed.com/files/centos6-epel-eol.repo --output /etc/yum.repos.d/epel.repo
yum -y install centos-release-scl-rh
# don't forget to fix repository configuration due to EOL:
curl https://www.getpagespeed.com/files/centos6-scl-eol.repo --output /etc/yum.repos.d/CentOS-SCLo-scl.repo
curl https://www.getpagespeed.com/files/centos6-scl-rh-eol.repo --output /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
# install python 3.6 from Software collection packages (installing virtualenv will pick up both the base python and its pip package)
yum -y install rh-python36-python-virtualenv
Now you can create a virtual environment with any packages from pip, but you have to activate the software collection in your shell first, example:
. /opt/rh/rh-python36/enable
cd ${HOME}
# create "myapp" virtual environment
virtualenv myapp
# install desired packages using virtualenv's pip (will install to the virtualenv and not damage your system)
./myapp/bin/pip install cymysql
Do not go over pip version 20 on a Centos 6 system, otherwise wheels with precompiled c libraries will break.
Long story short, do this first:
pip install --upgrade pip==20.1.0
After that you can install cymysql

Installing pip package through .ebextensions

I am having trouble installing the package psycopg2 through ebextensions.
00_software.config
packages:
yum:
git: []
htop: []
pip:
psycopg2: []
01_setup_pg_activity.config
commands:
clone:
command: git clone https://github.com/dalibo/pg_activity.git
install_psycopg2:
command: cd pg_activity && python setup.py install
Alternatively, I have also tried this to no avail
commands:
clone:
command: git clone https://github.com/dalibo/pg_activity.git
install_psycopg2:
command: pip install psycopg2
install_psycopg2:
command: cd pg_activity && python setup.py install
The other software installs but pip packages will not
Traceback (most recent call last):
File "/usr/local/bin/pg_activity", line 4, in <module>
__import__('pkg_resources').run_script('pg-activity==1.6.1', 'pg_activity')
File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 742, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1510, in run_script
exec(script_code, namespace, namespace)
File "/usr/local/lib/python2.7/site-packages/pg_activity-1.6.1-py2.7.egg/EGG-INFO/scripts/pg_activity", line 42, in <module>
ImportError: No module named psycopg2
I have to manually go in and run sudo pip install psycopg2 for it to correctly install. How can I do this in the build process?
If you are using a virtual environment and EB CLI, simply creating requirements.txt will be enough while the virtual environment is active. EB CLI handles the package installations on deployment.
Using EB with AL2 (python env; haven't tested on RoR), successfully installed pg_activity.
For packages (only psycopg2 shown) I had
packages:
yum:
python-psycopg2: []
And the commands config file was (20_setup_pg_activity.config):
commands:
05_clone:
command: git clone https://github.com/dalibo/pg_activity.git
10_install_pg_activity:
command: cd pg_activity && python setup.py install
Following the env setup, I logged into the EB instance to check if pg_activity exists (only part shown):
ec2-user#ip-172-31-82-127 ~]$ pg_activity --help
Usage: pg_activity [options]
htop like application for PostgreSQL server activity monitoring.
Options:
--version show program's version number and exit
-U USERNAME, --username=USERNAME

Unable to install any Python library in VS 2017: 'module' object has no attribute 'main' [duplicate]

I'm facing a problem with Package Installation, pip.
I am using Python 3.6.
When I try to install a package with pip I receive the following message in the cmd.
*C:\Python36\Scripts>pip intstall --user httpie
Traceback (most recent call last):
File "c:\python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python36\Scripts\pip.exe\__main__.py", line 5, in <module>
ImportError: cannot import name 'main'*
It happened when I upgraded the pip, before it worked. I tried to uninstal the Python and installed this again but it does not work. I cannot find the solution, any ideas?
Firstly if you have installed pip then remove it by deleting the folder of pip inside python directory. Then install pip by this command:
$ sudo easy_install pip
This will install older version, you can download newer version after 10th when it will come.
I fixed it. I deleted the folder with pip as you said. Because I work in Windows 10, I downloaded the get-pip.py from https://pip.pypa.io/en/stable/installing/ and I run it.
You can also downgrade to pip 9.x using
python -m pip install -U "pip<10"

No module named django.core.management,error while installing django in vm

When I try to run my server using:
./manage.py runserver 0.0.0.0:8000
I get the following error:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
After browsing for a solution, I learned that Django was not installed so I tried installing it on a VM.
I tried the following:
pip install django
yum install django
yum install python-django
I also tried running these commands with sudo -s and sudo -E.
I am getting the following when I use pip cmd:
Downloading/unpacking django
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement django
No distributions at all found for django
Storing complete log in /root/.pip/pip.log
Using Yum I get:
No package python-django available.
Error: Nothing to do
Install a new version of Python. Pip has to be installed for the new
version. CentOS 6.6 has default Python 2.6 and the default pip
doesn't work properly.
Install Django with new pip.
Do not install the new version of Python globally as CentOS relies on
Python 2.6 for yum.

Django installation on OSX

OS : OS X 10.7.5
I have pyhton 3.3.2 installed under Apps folder and I use IDLE for python scripts.
I used below command to install Django.
pip install Django==1.5.2
After successful installation I see it installed under ~/Library/Python/2.7/site-packages/
However when I use IDLE to test
import django
I get below error:
>>> import django
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import django
ImportError: No module named 'django'
What am I missing ? This is the first time I am trying django installation.
As per suggestion, I installed virtualenv and tried
sudo virtualenv -p /Library/Frameworks/Python.framework/Versions/3.3/ my_virtualenv
It failed with below trace:
Running virtualenv with interpreter /Library/Frameworks/Python.framework/Versions/3.3/
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 8, in <module>
load_entry_point('virtualenv==1.10.1', 'console_scripts', 'virtualenv')()
File "/Library/Python/2.7/site-packages/virtualenv.py", line 780, in main
popen = subprocess.Popen([interpreter, file] + sys.argv[1:], env=env)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
Seems like still its trying to install under 2.7 version.
You have a conflict between python 2.7 and 3.3.2. You installed django for python 2.7 and certainly tried to used it with python 3.3.2.
The best way to avoid this kind of problem is to use virtualenv:
$ sudo pip install virtualenv
Then:
$ virtualenv my_virtualenv
OR:
$ virtualenv -p <PATH TO PYTHON VERSION> my_virtualenv
Then:
$ source my_virtualenv/bin/activate
$ pip install Django==1.5.2
This will install the good version of django in your virtualenv. You need to check if the python 3 version is available with pip.
Thanks to virtualanv, you will be able to save/freeze and install your environement on another machine:
$ pip freeze > requirement.txt
$ pip install -r requirement.txt
Here are the steps that resolved my issue:
Removed Python 3.3 completely from machine.
sudo rm -rf /Library/Frameworks/Python.framework/Versions/3.3
sudo rm -rf "/Applications/Python 3.3"
cd /usr/local/bin;
ls -l . | grep '../Library/Frameworks/Python.framework/Versions/3.3' | awk '{print $9}' | xargs rm
And then setup the environment using guide to set Up Python and Install Django on Mac OS X.