I am working in ubuntu. I have installed the django==1.3 in root earlier. Now I created local virtual environment name as local_env using virtualenv. Then I activated the local_env using source command.But I didn't install the django in this local_env environment. When I try to create the django sample project using
django-admin.py startproject sampleproject
it is working perfectly. My question is,It didn't prevent the local_env from the root environment ?. I mean it didn't raise the error like django-admin.py command didn't found. Please put the comments if any doubts.
django-admin.py is in the OS path. When you try executing it inside your virtual environment, the python will look into it's VE's sys.path and if not found it will search through the OS path, found it from the root environment and so there were no errors shown.
According to the docs, the django-admin.py command is installed on your system path, so if the virtualenv doesn't find this command, it will probably look in the global sys path and find a match.
If you want to use a different version of django to the one installed on your sys path, you will need to install it in your vritualenv using a package manager like pip and this will then take precedent over the global django-admin.py
To make sure you're doing this right, load your the virtualenv using
dhanna#dhanna:~$ source local_env/bin/actviate
If successful, your prompt should have the virtualenv name at the beginning - eg
(local_env)dhanna#dhanna:~$
Note that if you activate the virtualenv in one shell, but run the python interpreter inside a separate shell, you will be using the global interpreter and therefore have the global django-admin.py module available.
Next you'll want to install the django module
(local_env)dhanna#dhanna:~$ pip install django
To check if django is installed inside your virtual env, you can use the package management tool pip and pass the freeze argument
(local_env)dhanna#dhanna:~$ pip freeze
Now you can use the virtualenv's version of django-admin.py
(local_env)dhanna#dhanna:~$ django-admin.py startproject sampleproject
Related
I just installed virtualenv and in it I installed django. However, when I go to the django-admin terminal in the bin file, I wrote
django-admin startproject mysite
I thought that would start a new project but it just returned
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
Here is how to start a new django project in a virtualenv:
1. Create a new virtualenv for your project:
virtualenv py_env --python=python3
--python=python3 is not mandatory. I'd recommend programming in python3.x but this is up to you. If you are unsure about what is the default python that will be used when omitting the --python option, type python -V in your terminal.
2. Activate the virtualenv:
source py_env/bin/activate
If you see a (py_env) at the beginning of the command line, then you know the virtualenv is activated. To deactivate, simply type deactivate.
3. Install the required packages:
pip install django
While this is not needed, I recommend using ipython, so you might want to run pip install ipython.
4. Create a new django project:
django-admin startproject mysite
Hope that helped and happy coding!
I think you should first make a virtual environment:
pip3 install --user pipenv
Make a virtual environment:
pipenv --python 3.6
Activate the environment:
pipenv shell
and after this do whatever you want i think it would work better now
Pipenv is best to use because it brings the best Packages of python so that there may not be further bugs while making the virtual environment. And django can be replicated properly.
I am working in a virtual environment in Python 3.I need to use a 3 party module "mglearn" and I copy it to my virtual environment's lib/:
/home/abigail/environments/my_env/lib/python3.5/site-packages/mglearn
However, in ipython command line, it can't find the module name:
In [1]: import mglearn
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-e19748f92cd9> in <module>()
----> 1 import mglearn
ImportError: No module named 'mglearn'
It should find it. Right?
Then I checked my sys.path:
In [4]: print(sys.path)
['', '/usr/bin', '/usr/lib64/python35.zip', '/usr/lib64/python3.5', '/usr/lib64/python3.5/plat-linux', '/usr/lib64/python3.5/lib-dynload', '/usr/lib64/python3.5/site-packages', '/usr/lib/python3.5/site-packages', '/usr/lib/python3.5/site-packages/IPython/extensions', '/home/abigail/.ipython']
Why does "sys.path" only contain directories starting from the root /, not my virtual environment? How can I get that module to be searched by Python?
Edited:
[abigail#localhost bin]$ ll activate
activate activate.csh activate.fish
[abigail#localhost bin]$ ./activate
bash: ./activate: Permission denied
[abigail#localhost bin]$ sudo ./activate
sudo: ./activate: command not found
Strange! why is that?
VirtualEnv creates a clone of a Python installation and bakes an additional path into sys.path that point to the site-packages directory of a given virtualenv.
When you launch your IPython, it is likely installed in your main Python installation and does not know about any additional virtual environments you have created.
If you install IPython into a virtual environment, it will know about the site-packages location for that virtualenv. Try and run:
which ipython
Then look at your ipython script and you will see it begin with either:
#!/usr/bin/python
or:
#!/home/abigail/environments/my_env/bin/python3
The first indicates a globally installed ipython and the second is an ipython that has been installed into a specific virtualenv.
FYI, you can add paths to a Python interpreter by exporting the PYTHONPATH environment variable:
```export PYTHONPATH=/home/abigail/environments/my_env/lib/python3.5/site-packages```
This would let you use a globally installed IPython with your virtualenv. However, the typical way to do this would be to install a second copy of IPython in your virtualenv and use that copy.
```/home/abigail/environments/my_env/bin/ipython```
The activate shell commands for a virtualenv only do two things:
Add the virtualenv Python interpreter to your PATH. So when you type python3 you run /home/abigail/environments/my_env/bin/python3 instead of /usr/bin/python3. It is this binary at /home/abigail/environments/my_env/bin/python3 which will automatically include the /home/abigail/environments/my_env/lib/python3.5/site-packages location on the sys.path.
Change your PS1 environment variable so your terminal has a prompt to remind you which virtualenv you are working in.
It is up to you to use the activate shell script or not (as it's just very simple helper script, you can adjust environment in whatever way makes sense for yo). If you are only using one virtualenv, you can add exports to your ~/.bashrc file instead, e.g.:
```export PATH=/home/abigail/environments/my_env/bin/:$PATH```
Would automatically make python3 run your virtualenv Python the same as running source activate within your virtualenv.
Generally speaking for a virtual environment you will want to do an install to get the module you are looking to import to pre-pend correctly in your path variable at virtual environment activation time. Consider trying this:
Since it looks like you already have a virtual environment set up, and it looks like you are using some form of Unix/Linux:
/home/abigail/environments/ $ source my_env/bin/activate
You should then see your terminal look something like:
(my_env) /home/abigail/environments
that means you have an active virtual environment.
Next you should install the module you want. I am assuming that module is available via pip install.
(my_env) /home/abigail/environments $ pip install mglearn
This should get you all set up. When you check your sys path you should now see at the front of it your virtual environement python stuffs. And your import error should go away.
You may need to delete out the copy of mglearn you dropped into the directories manually if things get stuck.
My Question is pretty Simple.
Whenever I use virtualenv environment in my Django project it gives the following error when executed this command
python manage.py runserver
File "manage.py", line 8, in
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
But when i use root and give the same command it seems to work perfectly. Why does this Happen? I am in the virtualenv so it means i am the root person by default.How can i fix this error?
I had seen many forums but couldn't find related to mine.
This happens because you have installed django in the system-wide Python interpreter; possibly by doing something like sudo pip install django.
Once you create a virtual environment and activate it - it contains no packages. The concept of a virtual environment is that it allows you to install Python packages without affecting the global Python installation.
So once you activate a virtual environment, you have to install packages in that virtual environment; so you should pip install django (note: without sudo) once you activate the virtual environment; like this:
$ virtualenv sample_env
...
$ source sample_env/bin/activate
(sample_env) $ pip install django
A virtual environment is a virtual environment for Python; it does not control what the user is that is logged into the system.
Finally, as a general rule - you should not be using root for development purposes as doing so can easily compromise your system.
Yup i have same error while running Django. I also have enabled virtual environment. But I was still getting error.
Solution to this problem is using this command to install any python package
python -m pip install django
This will definitely solve your problem. As it solved mine.
I've created a n new directory, a virtualenv and installed a django-toolbelt inside it. The django-version should be 1.8 but when I call 'django-admin.py version' it says 1.6. So when I start a new project it creates a 1.6. I thought virtualenv was supposed to prevent this. What am I doing wrong?
Edit: I think it has to do with the PATH (?). Like it's calling the wrong django-admin version. I'm on Windows 7. Still don't know how to fix it.
I came across this problem too. In the official document, I found that, in a virtual environment, if you use the command 'django-admin', it would search from PATH usually in '/usr/local/bin'(Linux) to find 'django-admin.py' which is a symlink to another version of django. This is the reason of what happened finally.
So there are two methods to solve this problem:
re-symlink your current version django-admin(site-packages/django/bin/django-admin.py) to 'usr/local/bin/django-admin' or 'usr/local/bin/django-admin.py'
REMIND: This is a kind of global way so that it will effects your other django projects, so I recommend the second method
cd to your_virtual_env/lib/python3.x/site-packages/django/bin/(of course you should activate your virtual environment), and then use 'python django-admin.py startproject project_name project_full_path' to create django project
Try and install Django into the virtual environment as well:
pip install django
It should install the latest version, you can also specify a particular version (let's say 1.8.2) if you need to:
pip install django==1.8.2
Either way you'll have the correct version of Django in your virtual environment and it should work as you expect then.
You can also use the following command to see what version you have installed:
pip show django
Update:
It seems that you have the correct version installed in your virtual environment, but for some reason your Windows 7 use the system Django installation instead while you use manage.py or django-admin.py directly. However, you can use python manage.py or python django-admin.py instead, which seems to work as expected (and use the virtualenv Django installation).
I had the same problem. Could be related to your zsh/bash settings.
I realized that using zsh (my default) I would get django-admin version 1.11 despite the Django version was 2.1! When I tried the same thing with bash I would get django-admin version 2.1 (the correct version). Certainly a misconfiguration.
So, I strongly suggest you check your zsh or bash settings to check for paths you might have.
Create a virtual environment for a project:
$ mkdir cd my_project_folder
$ cd my_project_folder
$ virtualenv venv
$ source venv/bin/activate
And now install django
(venv) ~$ pip install django==1.8
manage.py runserver in virtualenv using wrong django version
pip / virtualenv / django installation issue
I am using centos linux.
I had python 2.6 with django and now i upgraded to python 2.7.
Python 2.6 is located in /usr/lib/python2.6.
Python 2.7 is located in /usr/local/lib/python2.7.
They both have site-packages directory and they both contain django 1.2.
If i run python i get the 2.7 version.
My problem is that if try to import django i get
ImportError: No module named django
I am not sure where is my PYTHONPATH defined and if this is what i need to change.
anyone ?
i ended up making a symbolic link to the 2.6 site-packages directory.
I had the same error, and this fix my issue
python -m pip install django
:) Done!
To check your path, you can use the following code:
import sys
print(sys.path)
If you already know where django is installed, it should be easy to test if the desired directory is in your path with directory in sys.path.
Regarding where your PYTHONPATH is defined, note that it's an environment variable, so you can check its value (if defined) with: echo $PYTHONPATH
Under linux, you can set the PYTHONPATH environment variable in your .profile or .bashrc. You can either edit it directly from the terminal by changing to your home directory (cd ~), and then edit the file (nano .bashrc), or by opening the file with gtkedit or vim or whatever, and add:
PYTHONPATH=/usr/local/lib/python2.7/site-packages:/another/path/etc
If you want to test this before editing your profile, you can export this from the terminal as:
export PYTHONPATH=/local/lib/python2.7/site-packages
I'm assuming you're running this straight from the command line. If you're running it as a wsgi module in apache, you can add this to your syspath from your wsgi file as:
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
try
pip freeze
this command show which packages are installed in your system
then run with root privilege
pip install django
then create a new project with command
django-admin.py startproject mysite
then start your project
cd path/to/mysite
./manage.py runserver
in file wsgi.py add this lines
import os
import sys
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)
Try printing sys.path to see what's in your path. Django need to be in one of the dirs listed. Example on Windows:
>>> import sys
>>> for p in sys.path: print p
C:\Python27\Lib\idlelib
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
>>>
django went missing with an upgrade to python 3.7
pip3 install django
fixed the problem.
python3 -m django --version1
for me it was that^
If you are using a environment use:
$ <environment_location>/<environment_name>/bin/python manage.py runserver
I also had same error but easily solved it .those who are using version 4 and above of Django and python 3.0 can do the following(for windows)
pip install virtualenv #installs virtual environment in pc
py -m venv myvenv #creates virtual environment named myvenv inside folder
myvenv/Scripts/activate # activates the virtual environment
You are now ready to go.
pip install django # pip install django
python -m django --version # checks the version of installed django