python console error: ImportError: No module named 'django' - python

I am following the django quick install instructions.
I activated the venv
I did pip install django from inside the venv, with and without sudo and receive the same message.
To check the install:
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'django'
"did you...?"
No. I didn't. I didn't do anything extra. This is a new spawning of a vm (built by vagrant/puppet) that already has virtualenv installed. The only thing I had to do to prep this venv was to make it not use the systems python 2.7.8. virtualenv --python=/usr/bin/python3.4 myvirtualenv
(On a separate pyramid-based project, it would fail during setup (python setup.py development.ini would fail with invalid command name 'development.ini' I don't need that project for this project, but saying this here may reveal something about the install.).
So how do I make this go away?
Edit: clarified for answers from #catavaran & #Burhan Khalid

I suspect here is where you went wrong:
You created a virtual environment (virtualenv --python=/usr/bin/python3.4)
Next, you forgot to activate it; or you activated it and then you did sudo pip install django.
sudo pip will run the system wide pip, installing django for Python 2.7.8, rather than for the virtual environment.
If you exit the virtual environment and try to import django - it will work.
To fix the problem, make sure you activate the virtual environment and then do not use sudo to install packages.

Before installing/using django you should activate your virtualenv. And do not sudo while you call pip:
$ source ~/.virtualenvs/myenv/bin/activate
$ pip install django

Turns out the problem was permissions earlier in the setup.
It leads to Django being unable to complete it's install. pip install django would fail with a permissions error on a mkdir during the Cleaning up... segment of the install. And while all the files may have been installed, I suppose it wasn't completely configged.
sudo pip install django is no better because the django install now has su permissions which helps with the mkdir problem above, while potentially creating other problems.
After both installs, the python console import django would error.
The fix: the virtual env dirs need to be owned by the user that invoked the virtual envs.
sudo chown -R youruser:youruser /opt/myenv
In my case, puppet created the venv and dirs as the root user.

Related

Python on RasPi can't find installed module

I feel like this has to have been asked and solved already, but I couldn't find a solution that works for me. I pip3'd a python library, and verify it is indeed on my system.
pi#raspberrypi:~/Desktop $ pip3 install pyftpdlib
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pyftpdlib in /home/pi/.local/lib/python3.7/site-packages (1.5.6)
Then I try to import it but raspi cant find it...
pi#raspberrypi:~/Desktop $ sudo python3 FTPserver2.py
Traceback (most recent call last):
File "FTPserver2.py", line 1, in <module>
import pyftpdlib
ModuleNotFoundError: No module named 'pyftpdlib'
Huh?
When you run pip3 install without sudo, the package gets installed under /home/pi/.local/lib/python3.7/site-packages which is a user-specific location and packages installed there will not be accessible system-wide.
Then you run sudo python3 which makes you execute python3 as the root user which is a different user.
Below I assume you do need to run the command with sudo. If you're not sure, try dropping the sudo - then the import should work (but maybe other stuff will not - I don't know what's in your script).
One method of installing a package for use by root would be to do sudo pip3 install pyftpdlib but this not recommended as it could break the Python installation used by the OS (some packages could have to be updated in order to be compatible with pyftpdlib, but they could then become incompatible with other stuff, and then you're in a lot of trouble).
It is better to use a virtual environment. For example:
# create the virtual environment
$ python3 -m venv env-ftp
# install the package into it
$ env-ftp/bin/python -m pip install pyftpdlib
# run a script using the Python installation contained within the virtual environment
$ sudo env-ftp/bin/python -m Desktop/FTPserver2.py
You could also choose to source env-ftp/bin/activate in order to temporarily switch to using python and pip specific to this environment until you deactivate.
Virtual environments are useful for creating isolated Python installations with their own separate sets of packages, which allows you e.g. to simultaneously use applications that have incompatible sets of dependencies (suppose 1 application requires requests==2.22.0 and another one requires requests<=2.21.0 and won't work with requests==2.22.0).
Can you try running this in the command line without errors?:
python3 -c "import pyftpdlib"
If this returns no error, that means your python interpreter is different. Make sure you are not running different python versions and/or have created different images and have not used sudo privileges to install packages.
As you can see, the pip3 has installed it in the site packages of /home/pi/.local/lib/python3.7/
Run this in command line
python3 -c "import site; print(site.getsitepackages())"
and check if it returns the same path as pip.
PS: It is always recommended to run pip3 install --user instead of sudo pip3 install.

Install site-packages inside virtual environment python

I am working on deploying my django application using uwsgi and nginx on a RHEL with pre installed python packages. Server is installed with uwsgi and nginx globally(as root). My server is not connected to internet.
I have secure copied my django project inside this server. For best practices I am told to use virtual environment and when I do the command,
virtualenv -p /usr/local/lib/python3.5/bin/python3.5 venv
All is cool. It creates a virtual environment with python 3.5.
But the problem is, I need these site packages which is pre installed on the server into this virtual environment. Example django, redis etc. When I do the above command with --system-site-packages it throws me this error
PermissionError: [Errno 13] Permission denied:
'/usr/local/lib/python3.6/site-packages/wheel-0.29.0.dist-info'
Is there a cleaner way to do this or where is it going wrong `?
You may try:
$ pip freeze
where pip is the system-wide pip. If it outputs anything, then just do:
$ pip freeze > requirements.txt;
$ source /path/to/venv/bin/activate && pip install -r requirements.txt;
In this way you will tell the virtual environment's pip to install all the python packages that are installed system-wide.
If pip freeze doesn't work, this is a privilleges issue, you have to elaborate this as/with an admin.
UPD: You may need to install additional packages as python3-dev, build-essential etc for pip install -r requirements.txt to work.
This might be happening because of the existence of another user in your machine.Just check if you have the system privileges , if not edit the properties of the file and grant it permission.

Problems with installing Django with Python3

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>

python: how to install and use setuptools in virtual python

I do not have root previlege on a linux server so I want to creat a virtual python according to creating a "virtual" python.
After I run virtual-python.py, I do have python in ~/bin/python:
Then, according to setuptools PyPI page, I download ez_setup.py and run ~/bin/python ez_setup.py. Error occurs:
What should I do?
Looking at the linked website, it looks outdated. You use pip, not easy_install.
For installing development packages, I always take the following rules in account:
The system package manager is responsible for system-wide packages, so never use sudo pip. This doesn't just match the question, but this is always a good idea.
The package manager packages are probably outdated. You'll want an up-to-date version for development tools.
I recommend the following way to install local development tools.
$ # Install pip and setuptools on a user level
$ curl https://bootstrap.pypa.io/get-pip.py | python - --user
$ # Add the executables to your path. Add this to your `.bashrc` or `.profile` as well
$ export PATH=$PATH/$HOME/.local/bin
At this point pip should be accessible from the command line and usable without sudo. Use this to install virtualenv, the most widely used tools to set up virtual environments.
$ pip install virtualenv --user
Now simply use virtualenv to set up an environment to run your application in:
$ virtualenv myapp
Now activate the virtual environment and do whatever you would like to do with it. Note that after activating the virtual environment, pip refers to pip installed inside of the virtualenv, not the one installed on a user level.
$ source myapp/bin/activate
(myapp)$ pip install -r requirements.txt # This is just an example
You'll want to create a new virtual environment for each application you run on the server, so the dependencies can't conflict.

Do i need to install Django in virtualenv as well if i don't use system package

I have installed virtualenv with --no-site-packages option.
I have few doubts
If I use django with virtualenv then does it mean that my django site is completely cut off from system packages. I mean any of the package installed in system site packages won't be available?
I have installed all packages in virtualenv but not django. Do I also need to install Django in virtualenv as well?
Suppose I have some package which is not in virtualenv but that is available in main env, can I access it from main package or only one environment can run at one time?
Yes you do, you can do that via pip or download from Django and run setup. In both cases you need to make sure that you have the virtualenv active, i.e. source ENV/bin/activate
The point of virtualenv is to keep your main system separate, you want to do that.
yes.
you should just install them in your virtualenv, it is better practice.
A really nice thing about virtualenv is that you can create a nice complete environment for your project. Then once things are working and stable you can pip freeze the packages and git your code and then you know if you share your project or move systems the whole thing is going to be easy to recreate and just work :)
--- update to comment ---
at command line and assuming Linux type environment
$ cd
$ virtualenv --no-site-packages --distribute ENV
$ source ENV/bin/activate
$ pip install django
$ pip install all_the_packages_you_need
Now you can go into your django project and run python commands as normal and it will use your virtualenv "ENV" python and site-packages

Categories

Resources