Django-admin creates wrong django version inside virtualenv - python

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

Related

How to start a new project in django using virtualenv

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.

Should django-admin version be same as Django?

I tried to configure my PC for Django course , i have fresh Ubuntu 18.04 :
I followed all steps from the course :
I had newest python pre-installed, Python 3.6.5 so i went :
sudo apt install python3-pip
went ok, then :
pip3 install django==2.0.2 (version suggested by instructor)
that completed as well, then :
django-admin startproject wordcount
and that gave me error :
Command 'django-admin' not found, but can be installed with:
sudo apt install python-django-common
So i tried to install this but it didn't help. so i checked web for answers and i found that i should do :
sudo apt-get install python-django
And after that i could use
django-admin startproject wordcount
and it created a project for me but i saw that some elements of default files are different from what i saw on course video so i check and :
django-admin --version
1.11.11
also django-admin --help
gives me message like this :
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.).
Any ideas what went wrong and how I can fix it?
The problem is that the pip3-installed packages aren't on the PATH for some reason. There's a few ways to fix this.
Add the pip3 installation location to your $PATH.
Force it to use the version of Django you installed via pip3:
$ python3 -m django.bin.django-admin startproject wordcount
Use virtualenv or pipenv to create a "virtual" Python installation - complete with it's own Python version and set of libraries.
I see you struggle with this problem. I would recommend you to move to such thing as a virtualenv (or use anaconda for this task). This should solve a numerous other problems you've not ran across yet and this one especially.
Virtual environment allows you to create separate environment, with it's own packet set. Some IDEs (for example, pycharm) provide GUI for it's creation and management.
Update 1:
Thanks to #DhavalSavalia, of course there is also pipenv package, that is a more friendly virtualenv wrapper (unless you have some gui for virtualenv).
Update 2:
Also, I've noticed that you setting up django using apt. You probably shouldn't do that, because apt often contains old version of python packages. Use pip for as many packages as you can.

zsh: command not found: django-admin when starting a django project

I use Ubuntu 15.10 and zsh (don't know if it can help)
So I try to install django:
pip install django
Downloading/unpacking django
Downloading Django-1.9.5-py2.py3-none-any.whl (6.6MB): 6.6MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...
Everything works fine. When I do pip freeze I can see django is installed.
then:
django-admin startproject mysite
But I got this issue:
zsh: command not found: django-admin
I found an alternative solution.
With find / -name django-admin I found django-admin in myHome/.local/bin/django-admin.
So instead of django-admin startproject mysite I use the full path myHome/.local/bin/django-admin startproject mysite
thanks to #Evert, this is why I got the problem.
his comment:
This is likely because you either used the --user option to pip
install, or you set up pip in such a way that it automatically does
that. Hence, everything gets installed in $HOME/.local. You may want
to add $HOME/.local/bin to your $PATH for the future.
When I had the problem on my mac I just uninstall django and install it again but with root permissions. Now it's working good)
pip3 uninstall django
sudo pip3 install django
I faced a similar issue on Mac OS but I moved in another way. I used Virtual Environments.
First, create the virtual environment
python3 -m venv django-env
Then, use this environment
source django-env/bin/activate
Next, install django
python -m pip install django
Finally test django is working
django-admin startproject mysite
In my opinion, it is better to have environments isolated to avoid O.S. settings
I was facing the same problem after updating my Mac OS to Catalina, and shell from bash to zsh.
None of the commands would work, as if all my paths were deleted.
Looking at Brady Huang's answer, doing something similar worked for me.
I made sure django was installed correctly by reinstalling it
pip3 uninstall django
pip3 install django
I found django installed under:
/Library/Frameworks/Python.framework/Versions/3.8/bin/django-admin
by running:
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.8/bin/django-admin usr/local/bin
I was able to run django-admin again.
My adjango-admin is located in
~/Library/Python/3.7/bin/django-admin
But I don't have it in my linked PATH which is looks like this
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin
So I create a symlink to one of the bin I have in my PATH
sudo ln -s ~/Library/Python/3.7/bin/django-admin /usr/local/bin
It solves my issue
You may be having some dependency issues.
So to avoid it happen again you need to create a virtual environment only for the current project you are doing. Like this you can avoid having issues and isolate your application.
You can follow this question to create a virtual environment and add django in it.
Then, as soon as you create the virtual environment and activated you can pip install django and check to see whether it is installed.
Tried many stuffs but then installing the python version 3.11 and using a virtual env solved the issue. Earlier I was using 3.8.9. Hope this helps someone
what worked for me!
I just Added the following in my ~/.zshrc file( you can also add it in the ~/.bashrc file if you are not using zsh terminal)
export PATH=$PATH:$HOME/.local/bin
This solution will work not only for you django-admin command but all other python package commands that would behave in a similar way.

Is there a way to install django with pip to point to a specific version of python in virtualenv

I have a system with CentOS installed. It currently runs python2.6, but python2.7 is also installed.
I want to run django 1.7, which is also currently installed. If I run django outside of a virtualenv, it is using python2.6 by default. I didn't install it myself.
What I assume is a way to get around this is to create a virtualenv. Which I've done, and used --python=python2.7. But when I create the virtualenv, and install a new django 1.7 in it (with pip), it still uses python2.6 instead of 2.7.
Since I'm doing this all through ssh, I'd like an easy way around it (rather than compiling from source, etc). Is there a way to specify that django uses python2.7 when I install it with pip in the virtualenv? Or what is the right way to correct this issue?
Here is what I have done:
ssh into account.
$ mkdir project; cd project
$ virtualenv env --python=python2.7
$ cd env
$ source bin/activate
$ sudo easy_install-2.7 pip
$ pip install django==1.7
Then I go into my python interpreter. The interpreter is running 2.7 and if I import django, it all works okay. But as soon as I run
django-admin.py startproject project_name
it is back to using 2.6.
Invoke django-admin.py like this python django-admin.py, in your activate virtualenv. Alternatively you can do /path/to/virtualenv/bin/python django-admin.py.
The best solution is probably adding a shebang to django-admin.py that looks like #!/usr/bin/env python which should use the python interpreter of your active virtualenv. See https://stackoverflow.com/a/2255961/639054
You could use pyenv, which will allow you to set your Python interpreter based on what directory you're in. I also use it with pyenv-virtualenv so that I get all the benefits of isolating my packages per application.
There's a bit of a learning curve, but it's worth it if you need to switch Python interpreters to match a production server for instance.

Command not found: django-admin.py

I am a complete beginner to Python/Django, but I want to dive right in and start experimenting. Thus I was following this guide on installing Python/Django http://devcenter.heroku.com/articles/django.
Everything is working fine until the step
django-admin.py startproject hellodjango
Where I get
command not found: django-admin.py
Now I have tried a few things, but none have really worked out. Is there someone kind enough to point me in the right direction?
P.S. Is there a great guide out there on running Python/Django locally on a Mac to run and test apps?
I'm on Mac OS X Lion, Python 2.7.
When that didn't work for me, I tried python -m django startproject mysite and it worked.
Actually, if you use Ubuntu, it's just django-admin not django-admin.py.
Resides in /usr/bin
Probably the same thing on Mac.
You're using a Windows tutorial.
It may also tell you
python manage.py runserver
and that is actually
python ./manage.py runserver
I tried as the method, it worked.
pip uninstall django
sudo pip install django
django-admin startproject example
It worked well.
Make sure you properly did the source bin/activate command. If you skip that, or do it in a different terminal window, or close the window then re-open it, you won't be in the virtualenv and you won't have access to the django-admin.py command in your environment.
If you´re on Windows, here´s what worked for me (using pylauncher):
$ py -m django startproject myproject
To solve this problem, you need:
Find the main folder of Django, and find the django-admin.py file
Typically, the file is in <YOUR_DJANGO_FOLDER>/bin/django-admin.py
Create a link for this file
ln -s /bin/django-admin.py /usr/local/bin/django-admin
Type django-admin in your command to check if it works
As Timmy said above, it could just be that django-admin.py is not on your system path.
See here - https://code.djangoproject.com/wiki/InstallationPitfalls - for 3 possible causes with solutions.
To Windows users
using django-admin instead of django-admin.py worked for me in Windows.
For me this one worked
python3 -m django startproject mysite
Then it doesn't tell you that it created file you must check by yourself in your home
hardrive/user/urhome
python3 -m django startproject mango
I have used follwong command to install (/usr/local/bin) MAC OS
pip install django
django-admin startproject mysite
The following guide in official site https://code.djangoproject.com/wiki/InstallationPitfalls
First, you should find location of django-admin.py by
which django-admin.py
Example: in my case
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/bin/django-admin.py
You use sudo ln -s to relocate django-admin-py to /usr/local/bin
sudo ln -s /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/bin/django-admin.py /usr/local/bin/django-admin.py
after that change permission of the django-admin to be executable
sudo chmod +x /usr/local/bin/django-admin.py
Now you can use
django-admin.py startproject mysite
to create your django project
If you want to change django-admin.py to django-admin to look like more compact you can use
sudo mv /usr/local/bin/django-admin.py /usr/local/bin/django-admin
Hope this help for you !
This simply worked for me.
Install django on your virtualenv:
pip install django
And then run:
django-admin startproject myprojectname
I was just having the same problem, I just did an upgrade and that's it, it worked, I hope this is useful for future problems in Linux:
pip install --upgrade django
There may be a chance that your path is not correct. Ubuntu has a .local/bin folder which pip uses to install module binaries and you need it in your path to use django or any shell commands installed using pip.
Open ~/.bashrc or ~/.zshrc
Add the following line and save it
export PATH="/home/animesh/.local/bin:$PATH"
restart the shell with source ~/.zshrc
To windows users out there: I have faced this problem several times and here are the checkpoints:
When there is problem initiating a new project, make sure:
1) python is working in the command line (type in python and see if you get the console)
2) specify the full path of django-admin.py in the command
3) check django-admin.py is in the system path
4) cd the command line path to where you want the new project
Screenshot of what finally worked for me (only the last command):
https://flic.kr/p/r9LJ67
(stackoverflow doesn't allow me to post pictures yet)
If you come across command not found: django-admin.py problem which means you don't installed django frame work. You should install the framework using pip.
pip install django
After that look at the directory if the related script exist or not.
Look into C:\Python27\Scriptsfolder to check for django-admin.py exist or not.
if you install django by pip
ensure you have installed django:
pip list or pip freeze
if there is django
then
get location of django:
pip show django
if location is '/Users/xxxxx/Library/Python/2.7/lib/python/site-packages' then
relocate django-admin-py to /usr/local/bin:
ln -s /Users/xxxxx/Library/Python/2.7/lib/python/site-packages/django/bin/django-admin.py /usr/local/bin/django-admin.py
Get site-package path with Python:
import site; site.getsitepackages()
And run django-admin.py directly:
python (your-site-package-addresss)/django/bin/django-admin.py startproject hellodjango
On Mac: If this works you can go and add django-admin.py to your path using symlink:
sudo ln -s (your-site-package-addresss)/django/bin/django-admin.py /usr/local/bin/django-admin
(could be that you have to reopen terminal or reinstall django to get the symlink working)
I changed my complete python path to desktop and tried and it's working well for me
In my case i simply forgot to run pip install django
On RHEL stock python config:
django-admin.py startproject mysite
I'm running macOS and I'm using pyenv instead of virtualenv. I'm not sure if they behave similarly, but I was having the same problem in which django-admin.py was not found.
After a while I've noticed that I had a warning after installing django:
pyenv: cannot rehash: /Users/msvolenski/.pyenv/shims/.pyenv-shim exists
Once I deleted this file and ran pyenv rehash it all started working perfectly.
Hope this helps!
For Windows Users first search for django-admin, right click on the file that has been found and open file location and keep it open.
Using Windows Powershell, cd into the the folder where you want to create your django project
when your in the right folder write the full path of where django-admin is located in my case I am using Anaconda 3 so the file location is
C:\Users\Sen\Anaconda3\Scripts
so in Windows PowerShell type C:\Users\Sen\Anaconda3\Scripts\django-admin.py startproject [name of project]
hope this helps!
I had the same issue when migrating to AWS Beanstalk it was installed and everything but i noticed the alias was not working but when i called the entire thing path and all it worked so i just rebooted the boxes and it worked i think the alias list is not updated automatically after you install.
It has to do with the PATH:
Put this in the .bash_profiel and the source it (for mac users only):
(change the location with the location of your installed python libraries)
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
If someone is facing the same problem, and is on MacOs, here is what I did, and it worked for me:
If you've installed python directly from the official website, uninstall it, and install it once again using brew:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install python3
This will also install the pip3 for you, so you don't have to install it by yourself.
I was facing the same issue.
The issue resolved after I upgraded the django version using
pip install --upgrade django
Then run
django-admin startproject mysite
Hope this helps!
There was a space in one of the names in the path to my project. I set up a new virtual environment in a new directory and did all the same things and it works.
Sometimes it is the simple things...
Since you're just starting out. It is very important to adopt best practices. You will face many dependency related issues with this approach of development. In this case it is always recommended to work with a virtual environment for each python project.
This will ensure fresh installation of project-specific dependencies that do not overlap with what the system you are running on already has.
If you have not already noticed, you will come across more issues such as python3 not working with earlier versions of django. pip will skip install as it checks and finds a version of django already installed. So this cannot be stressed enough, always use a virtual environment for local setups.
to do so:
cd [your project path]
virtualenv venv
you can active your environment by :
source ./venv/bin/active
install your requirements packages with pip :
pip install -r
or pip install
you can also install your requirements modules without activate the environment
./venv/bin/pip install
to run your python script use :
python <.py file>
and if you didn't activate your env use :
./venv/bin/python <.py file>

Categories

Resources