pip install: How to force a specific package version - django

I'm trying to install Django 1.4.3, but when I execute pip install, pip keeps installing Django 1.5 version instead 1.4.3
sudo pip install -I Django==1.4.3
It returns:
Downloading/unpacking Django==1.4.3
Running setup.py egg_info for package Django
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
=== >>>> Requested Django==1.4.3, but installing version 1.5 <<<< ====
Installing collected packages: Django
Found existing installation: Django 1.5
Uninstalling Django:
Successfully uninstalled Django
Running setup.py install for Django
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
changing mode of /usr/local/bin/django-admin.py to 755
Successfully installed Django
Cleaning up...
but if I execute pip freeze, it keeps showing
Django==1.5
What am I doing wrong?
Thank you

This could/should/can be helped by clearing the build dir for Django in pip.
There is a bug for this, since version 1.1 see here for details
You can start checking for these folders here if you're on OS X or unix like systems:
~/.pip
/tmp/pip-build-root (or pip-build-$USER, if you aren't running pip as root).
This is if you haven't specified a new build folder when you installed the first version of Django.
Good luck!

As limelight says, you should empty your cache and build directories, or pass in a temporary clean location with the --download-cache and flag.
$ pip help install
[...]
--download-cache <dir> Cache downloaded packages in <dir>.
-b, --build <dir> Directory to unpack packages into and build in. The default in a virtualenv is "<venv path>/build". The default for global installs is
"<OS temp dir>/pip-build-<username>".
I'd like to warn any readers to not use sudo pip install to install Django. It installs Django system-wide. And changing the system-wide version could break system-packages that depend on it. For instance, Ubuntu MAAS and Cobbler depend on the system django package. These are typically services you don't want to break.
If you need a different version than the system-package, use virtualenv to isolate your dependencies from the system.
OP seems to be on OSX and I don't know of any server-wide Mac Django, but that may change. Consider installing python packages with sudo at par to changing the system-installed python with python 3; it might work for now, but have some paracetamol in stock, as you're in for some headaches.

Check your local cache and remove it can be help. I hava installed pymongo==2.5.2. To install pymongo==2.4.1, I remove the cache in /tmp/pip-build-root/pymongo.Then I install pymongo 2.4.1 successfully.

Related

How to install pip on Python 2.7 in 2021

I have legacy production servers that are still running Python 2.7.6. We have a local environment built from the docker image for ubuntu 14.04 intended to replicate that environment (things still work there once everything is installed.) The packer build script that creates this environment recently stopped working apparently due to PyPi dropping non-SNI support.
I tried using get-pip.py from the docs to download pip:
wget -c https://bootstrap.pypa.io/pip/2.7/get-pip.py
python2 get-pip.py
This gives me the following warning:
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
/tmp/tmpBb3LJu/pip.zip/pip/_vendor/urllib3/util/ssl_.py:424: SNIMissingWarning: An HTTPS request has been made, but the SNI (Server Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
/tmp/tmpBb3LJu/pip.zip/pip/_vendor/urllib3/util/ssl_.py:164: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
ERROR: Could not find a version that satisfies the requirement pip<21.0 (from versions: none)
ERROR: No matching distribution found for pip<21.0
The proposed solution for that is to use pip to upgrade urllib3
https://serverfault.com/questions/866062/easy-install-and-pip-fail-with-ssl-warnings
I don't have pip so I installed a legacy version using
apt-get install python-pip
This installs pip 1.5.4
When I try to pip install "urllib3[secure]" I get the following:
Requirement already satisfied (use --upgrade to upgrade): urllib3[secure] in /usr/lib/python2.7/dist-packages
Installing extra requirements: 'secure'
Cleaning up...
If I try pip install "urllib3[secure]" --upgrade or pip install --index-url https://pypi.python.org/simple/ --upgrade pip I get:
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement urllib3[secure] in /usr/lib/python2.7/dist-packages
Downloading/unpacking urllib3[secure]
Cleaning up...
No distributions at all found for urllib3[secure] in /usr/lib/python2.7/dist-packages
Storing debug log for failure in /root/.pip/pip.log
(the pip message reflects pip, not urllib3[secure])
When I try to use pip 1.5 to install uWSGI
pip install uWSGI
I get the following:
Downloading/unpacking uWSGI
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement uWSGI
Cleaning up...
No distributions at all found for uWSGI
Storing debug log for failure in /root/.pip/pip.log
Upgrading pip doesn't work here either
Downloading/unpacking uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
Cleaning up...
No distributions at all found for uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
Storing debug log for failure in /root/.pip/pip.log
Reinstalling pip doesn't work:
python -m pip install -U --force-reinstall pip
Gives me:
Downloading/unpacking pip
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement pip
Cleaning up...
No distributions at all found for pip
Storing debug log for failure in /root/.pip/pip.log
If I open /root/.pip/pip.log I see the following:
Downloading/unpacking pip
Getting page https://pypi.python.org/simple/pip/
Could not fetch URL https://pypi.python.org/simple/pip/: 403 Client Error: [[[!!! BREAKING CHANGE !!!]]] Support for clients that do not support Server Name Indication is temporarily disabled and will be permanently deprecated soon. See https://status.python.org/incidents/hzmjhqsdjqgb and https://github.com/pypa/pypi-support/issues/978 [[[!!! END BREAKING CHANGE !!!]]]
The link says that SNI support was dropped:
For users of Python 2.7.{0...8}
Upgrading to the last Python 2.7 release is an option.
However, note that Python 2.7 series itself is now End of Life and support in pip was dropped with version 21.0.
For users of Python 2.6.x and lower:
Neither the Python core developers, or pip maintainers support Python 2.6 and below.
If someone is aware of a work around for this issue (SNI support specifically) they are welcome to share it here for others.
There is no recommended solution from the PyPI team.
How can I get a local environment set up for new developers to work on our legacy application? I've created a new Python 3 dev server and local environment but it will be some time before I can roll out the staging and live environments, get everything moved over, and test it.
As the message says, PyPi has discontinued support for Python <2.7.9 as of May 6th 2021. If you're running a version < 2.7.9 and you cannot upgrade to a newer version of Python then your only option is to manually download the wheels from PyPi.
These are the modification I needed to make to my build script to make it work:
I needed to install software-properties-common and gcc
apt-get install -y software-properties-common gcc
Then I downloaded (setuptools](https://pypi.org/project/setuptools/44.1.1/#files) and unzipped and installed it:
python ./setuptools-44.1.1/setup.py install
Next, I downloaded pip and added it to a folder called wheels. Then I could use the whl file to run pip to get pip
python ./wheels/pip-20.3.4-py2.py3-none-any.whl/pip install --no-index --find-links ./wheels/ pip --ignore-installed
It was suggested to build a Docker container using Ubuntu 16.04 with Python 2.7.17 and use that to download the packages.
pip download -r requirements.txt
But the versions of the packages were wrong, so I ended up going through the requirements.txt and downloading each package manually from PyPi and adding it to the wheels folder. A running instance is useful so you can run pip freeze or look at a requirements.txt file to grab the version numbers of all the packages you need.
Now that I could use pip, I can install my other packages:
python pip install --no-index --find-links ./wheels/ -r /root/requirements.txt
This uncovered some dependencies that I hadn't downloaded packages for yet so I had to go through and download those and added them to the wheels folder. There were a few other things I found needed different versions than I had originally downloaded and a few packages relied on pbr and many more wanted wheel:
pip install --no-index --find-links ./wheels/ pbr==5.5.1 wheel==0.36.2
I also needed to download cMake and add it to the wheels folder
After that I could install my requirements.txt:
pip install --no-index --find-links ./wheels/ -r /root/requirements.txt --ignore-installed
May be late to the party but something similar happened to me while trying to make an HTTPS request with Python 2.7.6 (lack of SNI support). This was causing a lot of issues on a remote web server I work on.
Looking for answers I tried installing urllib3[secure] and entered a loophole since pip was complaining about a lack of SNI support to install this and other packages as well.
I found out this StackOverflow answer which helped me install the required dependencies to make Python 2.7.6 and pip itself support SNI as well as install urllib[secure].
You need to create a folder containing the required wheels (download them from PyPi using wget for instance):
pip, asn1crypto, enum34, idna, six, ipaddress, pyOpenSSL, cffi,
cryptography wheels; and also pycparser (a non-wheel, it will be a
tar.gz)
Make sure the wheels you download support Python 2.7 and that you install pip before the rest of them.
In the original answer, its stated you can use python -m OpenSSL.debug to verify everything worked correctly (a ModuleNotFoundError would mean the pyOpenSSL package was not installed). You can also use pip -Vto check that the new pip version was installed correctly as well.
After updating pip and installing these dependencies I was able to install urllib3[secure] and get SNI support from python as well as pip.
Good luck!
I am sharing this answer as an update to Jonathan Rys's answer that contains the steps required as of the date of this answer. I tried to keep this concise.
As the message says, PyPi has discontinued support for Python <2.7.9 as of May 6th 2021. If you're running a version < 2.7.9 and you cannot upgrade to a newer version of Python then your only option is to manually download the wheels from PyPi.
For Ubuntu 20.04, I have installed build-essential sudo apt-get install build-essential
I installed Python 2 from source, downloading tar bundle and built and installed this. Note, I removed the python command, to avoid that old confusion, so we have python2 and python2.7 and also python3 (for example).
tar xf Python-2.7.18.tgz
cd Python-2.7.18
./configure && make && sudo make install
(cd /usr/local/bin;sudo rm python python-config)
cd ..
Now to get pip installed. Download setuptools zip archive. Then install it:
unzip setuptools-44.1.1.zip
cd setuptools-44.1.1
python2 bootstrap.py
sudo python2 setup.py install
cd ..
Next, I downloaded pip .tar.gz archive. Then unpack and install it. Note, I took extra steps to preserve and restore the original python3 pip in /usr/local/bin. Pip for Python2 are still available as pip2 and pip2.7 in the same directory.
tar xf pip-20.3.4.tar.gz
cd pip-20.3.4
(cd /usr/local/bin;sudo mv pip pip-save)
sudo python2 setup.py install
(cd /usr/local/bin;sudo mv pip-save pip)
Now that I could use pip, I can install the most important package any Python user should install, ipython. Note that I do a user install for this and also preserve my "ipython" command to run python3 and have ipython2 to run python2:
pip2.7 install ipython
(cd ~/.local/bin;rm ipython;ln -s ipython3 ipython)
and it works!
$ ipython2
Python 2.7.18 (default, Jun 22 2022, 09:38:45)
Type "copyright", "credits" or "license" for more information.
IPython 5.10.0 -- An enhanced Interactive Python.

Error installing django-heroku Error: pg_config executable not found

I tried installing django-heroku via pip so I can deploy my project on heroku but during installation of django-heroku it tried to install psycopg2 along only to produce an error:
Error:pg_config executable not found.
pg_config is required to build psycopg2 from source.please add the directory containing pg_config to the $PATH or specify the full executable path with the option:
python setup.py build_ext--pg-config/path/to/pg_config build...
or with the pg_config option in 'setup.cfg'.
And I also wish to know if I'm required to pip install it since heroku installs the libaries specified in the requirements.txt file,if not required are there changes I need to make to my project before deployment.
Perhaps not optimal solution but it did work for me.
pip install --no-dependencies django-heroku
Be sure to have other required packages installed.
pip install django
pip install whitenoise
pip install psycopg2-binary
P.S. I have already had postgresql installed on my Mac. Not sure why I was getting the same error.
I renamed "psycopg2" in the python site packages folder to "psycopg2-binary", and this fixed my issue.

Can't find any modules in Django project

I've been following a Django tutorial and initially created virtualenv and a requirements file in it. Project was halfway and in working state. Today I activated virtualenv successfully and tried python manage.py runserver to get error
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?
I realized it's not finding Django and just to be sure checked my requirements.txt file (which confirmed right Django version). Tried to reinstall it from the file using pip3 install -r requirements.txt (tried using just pip too) to get another error --
ModuleNotFoundError: No module named 'pip'
It seems the environment is unable to find any of the modules. I tried to reinstall pip also just to be sure, but then it couldn't find apt-get command.
OS: Mac OSX El Capitan 10.11.6
Python: 3.6.2
Django: 1.10.3 (in requirements file)
Try running python -m ensurepip (or python3 -m ensurepip) to see if pip is already installed:
In most cases, end users of Python shouldn’t need to invoke this module directly (as pip should be bootstrapped by default), but it may be needed if installing pip was skipped when installing Python (or when creating a virtual environment) or after explicitly uninstalling pip.

Failed building wheel for python-ldap (windows) *first stackoverflow ask*

I've installed flask, flask_login, and ldap in the virtualenv for my flask package (to authenticate ldap users with the usernames & passwords of the other users at my company).
When I run my flask app, I get "AttributeError: 'module' object has no attribute 'initialize'"
I'm tried "pip install python-ldap"
but I get a red error (sandwiched between other white text) that reads:
"Failed building wheel for python-ldap"
I have thoroughly examined these:
How to install python-ldap on a python 2.7 virtualenv on windows without compiling
Installing python-ldap in a virtualenv on Windows
https://www.python-ldap.org/docs.html
https://www.linuxjournal.com/article/6988?page=0,1
to no avail.
I've had this error for days now...
(also:
pip install openldap
gives:
Collecting openldap
Could not find a version that satisfies the requirement openldap (from versions: )
No matching distribution found for openldap
...(in and outside of my venv) if that information helps)
I had this error last week and after switching work computers, I recreated the virtualenv and installed all of the other packages and dependencies successfully.
my requirements.txt file contains these lines after a line of git stuff:
click==6.7
Flask==0.12.2
Flask-Login==0.4.0
itsdangerous==0.24
Jinja2==2.9.6
ldap==1.0.2
ldap3==2.2.4
MarkupSafe==1.0
pyasn1==0.2.3
Werkzeug==0.12.2
Has anyone successfully installed python-ldap into a flask virtualenv with windows? If so... how?!
Go to:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap
download python_ldap-2.4.38-cp27-cp27m-win_amd64.whl in the same directory that your venv folder is listed under
(ex.)
/myrepo
/myapp
/myapp
/venv
/run.py
/python_ldap-2.4.38-cp27-cp27m-win_amd64.whl
install the .whl file while inside of your venv
venv\Scripts\activate
pip install python_ldap-2.4.38-cp27-cp27m-win_amd64.whl
Voila!
The error
ERROR: Failed building wheel for python-ldap
can occur for several reasons. You actually have to look at what happened above this line. In my case, I just had to install missing dependencies to build python-ldap. You can look them up here. For CentOS 8, I had to install:
sudo dnf install openldap-devel python3-devel gcc
to make
python3 -m pip install --user python-ldap -U
work.

django install warning and manage.py runserver failure

I'm trying to work through the django tutorial, and seem to have a problem with the django installation (on Windows 7). I've installed Python, and pip, and installed Django via pip, at which point I got this output:
Downloading/unpacking Django
Downloading Django-1.5.1.tar.gz (8.0MB): 8.0MB downloaded
Running setup.py egg_info for package Django
warning: no previously-included files matching '__pycache__' found under dir
ectory ''
warning: no previously-included files matching '.py[co]' found under direct
ory '*'
Installing collected packages: Django
Running setup.py install for Django
warning: no previously-included files matching '__pycache__' found under dir
ectory ''
warning: no previously-included files matching '.py[co]' found under direct
ory '*'
Successfully installed Django
Cleaning up...
Since the messages were only warnings, I proceeded with trying to run django From within the project folder, I issue:
C:\data\spark2\spark\spark_project>python manage.py runserver
and receive the message:
ImportError: No module named debug_toolbar
then the command prompt is displayed again.
I've tried uninstalling and re-installing Django, to no avail.
Any ideas?
You forgot to install django-debug-toolbar which your project requires.
Running pip install django-debug-toolbar should fix that import error.
Ideally projects come with a Requirements.txt file that contains the output of pip freeze. If you have one, you can install all of the requirements by running pip install -r Requirements.txt