Problems with installing Django with Python3 - python

I have installed virtualenv
sudo pip3 install virtualenv
Then created a new virtualenv
virtualenv --python=/usr/bin/python3.5 venv
It returned
Running virtualenv with interpreter /usr/bin/python3.5
Using base prefix '/usr'
New python executable in /home/alexandra/Documents/online-store/venv/bin/python3.5
Also creating executable in /home/alexandra/Documents/online-store/venv/bin/python
Installing setuptools, pip, wheel...done.
Activated it
source venv/bin/activate
Then installed Django
sudo pip3 install django
It installed there (that is what sudo pip3 install django --upgrade returned)
/usr/local/lib/python3.4/dist-packages
Maybe that's the problem? Because it installed in python3.4 and not python3.5. But how to install Django in python3.5?
In file manage.py there is a path to python3.5 in this virtualenv.
Then I run
python manage.py runserver
It returns an error
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'
Can anyone help me?

When using pip to install packages inside the virtual env, do not sudo. It makes packages be installed globally. Simply remove sudo from beginning of the command and it should install it in the venv you are currently working on.

You installed django into a system python 3.4.
When you ran install command first time without sudo, did it complain about permissions? That should have been your clue that it's not the right pip. My guess is pip3 only exists in system python and not your virtualenv. You can use which to find out, but this will work in any case:
venv/bin/pip install django

Don't do sudo and just use pip for installing all the packages. You have already installed python3 in your virtualenv, so there is no need of using pip3.
Just do e.g. pip install django. or for other packages pip install <package_name>

Related

Errors to use pipenv after updating python version

recently I have updated my default python to be 3.10 but I realized that some parts of my OS started to broke;
I can use python -m venv venv and it works like a charm, but if I try to activate a pipenv shell I do receive some errors
It has started by raising errors in the collections.MutableMapping so I changed it to collections.abc.MutableMapping... But it doesn't stop raising errors and I can't remove the pipenv.
When I try to pipenv shell or even pipenv --rm I do receive the same message error
ImportError: cannot import name 'InvalidHeader' from 'requests.packages.urllib3.exceptions' (/usr/lib/python3/dist-packages/pipenv/vendor/requests/packages/urllib3/exceptions.py)
As I can't find references on how to fix this, any help you can provide will be really appreciated guys.
Regards,
Leonardo
Running with a new Ubuntu 22.04 system, I faced the same urllib3 collections issue you report as your initial problem:
$ pipenv install
Traceback (most recent call last):
<snip>
File "/usr/lib/python3/dist-packages/pipenv/vendor/requests/packages/urllib3/_collections.py", line 2, in <module>
from collections import Mapping, MutableMapping
ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)
I found a discussion of this in the pipenv github's issue page here:
https://github.com/pypa/pipenv/issues/5088
Apparently there is a problem with Python 3.10 with the apt-installed version of pipenv. I suppose the apt repositories should obtain an updated pipenv? In any case, taking the input from amod-amatya, I installed pipenv via pip (on my system, my packages are installed via sudo, but running as a non-sudo user should work as well):
sudo pip install --upgrade pipenv
FWIW, I didn't uninstall the apt version of pipenv. With that pip-installed pipenv version, pipenv now works for me:
$ pipenv --version
pipenv, version 2022.5.2
$ pipenv install
<snip>
Building requirements...
Resolving dependencies...
_ Success!
Updated Pipfile.lock (1d3b5e)!
Installing dependencies from Pipfile.lock (1d3b5e)...
__ ________________________________ 3/3 _ 00:00:01
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

I used pip3 to install virtualenv but I can't create venv and also I can't uninstall virtualenv

I'm using Linux Mint 20.2 with two directories / and /home.
I used the following command to install virtualenv:
>>> sudo pip3 install virtualenv
It worked fine and it installed in the following path:
>>> virtualenv --version
virtualenv 20.0.17 from /usr/lib/python3/dist-packages/virtualenv/__init__.py
But when I tried to create an environment I got the following error:
>>> python3 -m venv article
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/username/article_tools/article/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
When I tried to uninstall it to install it using [b]sudo apt install python3.8-venv[/b], I got the following error:
>>> sudo pip3 uninstall virtualenv
Found existing installation: virtualenv 20.0.17
Not uninstalling virtualenv at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'virtualenv'. No files were found to uninstall.
How can I fix it? By fix, I mean installing virtualenv in a way that I don't get such errors.
The fundamental problem here seems to be that you are mixing up two different packages.
Python 3 comes with a built-in virtual environment module venv which is however not installed by default on Debian-based platforms. Like the error message says, apt-get install -y python3-venv will install this package, which you can then use with python3 -m venv.
virtualenv is a separate third-party package which you invoke with the command virtualenv. It's not a bad alternative, but if you are only just learning, I would suggest you simply ignore it for the time being.

Pip inside virtualenv install packages in the wrong place

So I tried to install flask package inside virtualenv.
$ virtualenv env
Using base prefix '/usr'
New python executable in /home/egor/Documents/python/projects/flask/env/bin/python3
Also creating executable in /home/egor/Documents/python/projects/flask/env/bin/python
Installing setuptools, pip, wheel...done.
$ . ./env/bin/activate
(env) $ sudo pip3 install flask
After this I tried to import flask package in python and got ImportError exception. (env) $ pip3 freeze gave me nothing.
Then I've discovered package in /usr/local/lib/python3.5/dist-packages directory.
So basically pip3 inside vurtualenv install packages in the wrong place.
How can I solve this problem? Is it because of Using base prefix '/usr' line?
I believe that pip doesn't need root privilege (especially in a virtual env). Try pip3 install flask without sudo.

Error With Installing Virtualenv With Pip and Homebrew

I am trying to get a nice clean Python environment setup on OSX 10.9. I've installed Python with Homebrew and set my PATH variables so...
> which python
/usr/local/bin/python
and
> which pip
/usr/local/bin/pip
so when I look at my /usr/local/bin :
pip -> ../Cellar/python/2.7.6/bin/pip
python -> ../Cellar/python/2.7.6/bin/python
then when I run:
> pip install virtualenv
I get permission errors on /usr/local/bin/virtualenv:
...
running install_scripts
Installing virtualenv script to /usr/local/bin
error: /usr/local/bin/virtualenv: Permission denied
I thought that by using Homebrew I could use pip and avoid using sudo to install virtualenv. Am I doing something wrong here?
Ok! I managed to fix this myself.
I deleted all the virtualenv related things from /usr/local/bin (they had been installed under root for some reason and this was causing my permission issues.).
Then I did a pip uninstall virtualenv to get rid of other instances of virtualenv, as there was still one in /usr/local/lib/python2.7/site-packages/
Then a simple pip install virtualenv and things work fine now!
Most likely HomeBrew does some magic so that running brew install allows writing to /usr/local/bin, but this privilege is not available to normal commands. This is a guess, I didn't investigate this further.
Install virtualenv with brew:
brew install pyenv-virtualenv
This command:
pip install virtualenv
runs pip from your first directory from $PATH environment variable, which is usually system wide, thus when you run in this way - you are trying to install it globall.
You should install you your environment in your $HOME directory:
virtualenv $HOME/myvirpython
and later:
$HOME/myvirpython/bin pip install something
Additionally you should read this:
https://docs.brew.sh/Homebrew-and-Python

Errors in my Django install

I'm a beginner to Django so please excuse my ignorance here. I am trying to manually install Django so that I can start working with it. I've already installed Python successfully and downloaded Django to my Mac.
When I run the command "sudo python setup.py install" in the Django-1.5.1 directory, I get the following response:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'setup.py': [Errno 2] No such file or directory
Can anyone help me understand why this isn't running correctly and what I can do to fix it?
Thanks,
Ryan
As Nathan said pip or easy_install is the best way to install packages. But you need to make sure you have pip installed first:
sudo easy_install pip
sudo pip install django
If you need to specify your version of Django you can do it this way:
sudo pip install django==1.5.1
But normally it's better not to install packages globally (with sudo) and instead it's better to use virtual environments:
sudo easy_install pip
sudo pip install virtualenv
sudo pip install virtualenvwrapper
source /usr/local/bin/virtualenvwrapper.sh
mkdir myproject
cd myproject
mkvirtualenv --no-site-packages myprojectenv
workon myprojectenv
pip install django
I would suggest installing it via pip:
sudo pip install django
or easy_install:
sudo easy_install install django
Both methods will use the Python cheeseshop to install the latest version of django.
Take a look at https://docs.djangoproject.com/en/dev/topics/install/#installing-an-official-release-with-pip for more details.

Categories

Resources