18

I cloned my Django Project from Github Account and activated the virtualenv using famous command source nameofenv/bin/activate And when I run python manage.py runserver

It gives me an error saying:

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?

4
  • Are you sure it's installed? How did you install it? Commented Oct 8, 2016 at 17:09
  • I just activated virtualenv and tried to use python manage.py runserver command, I think after activating virtualenv there is no need to install any dependencies or is it ? I don't know much! Commented Oct 8, 2016 at 17:20
  • But you still need Django from somewhere! How are you expecting it to be in your virtualenv if you don't install it? Commented Oct 8, 2016 at 17:38
  • I was thinking that every and each dependency I need, might be present inside virtualenv. If that is not the case then I was wrong! This means I have to install all related dependencies of that project? Commented Oct 8, 2016 at 18:57

5 Answers 5

23

I was thinking that every and each dependency I need, might be present inside virtualenv.

Well, no. By default, a newly created virtualenv comes empty, that is, with no third-party library. (Optionaly, you may allow a virtualenv to access libraries installed system-wide, but that's another story.)

Once the virtualenv is created, you need to install the dependencies you need.

(How could virtualenv know what dependencies you need?)

The procedure is to install the virtualenv, activate it, and then install the libraries needed for the project (in you case Django and perhaps others).

If you project has a requirements.txt, you may install every required dependency with the command:

pip install -r requirements.txt 

If your project has a setup.py, you may also execute

pip install -e path/to/your/project/clone/. 

to install the project in the virtualenv. This should install the dependencies.

Of course, if the only dependency is Django, you can just type

pip install django 
Sign up to request clarification or add additional context in comments.

2 Comments

Do you mean installing django into the venv folder that is "C:\Users\John Uzoma\AppData\Local\Programs\Python\Python35\Lib\venv" ?
Well, activate the newly created venv, and from there, execute pip install django. Don't copy folders manually.
14

on ubuntu version

#install python pip sudo apt-get install python-pip #install python virtualenv sudo apt-get install python-virtualenv # create virtual env virtualenv myenv #activate the virtualenv . myenv/bin/activate #install django inside virtualenv pip install django #create a new django project django-admin.py startproject mysite #enter to the folder of the new django project cd mysite #run the django project python manage.py runserver 

7 Comments

Sorry bro, I forgot to mention that I am working on MacOS. thanks for answering :)
ok when activating your virtualenv if you run pip freeze command it must return smth like django== your django version try to install django in your virtualenv via command pip install django
@abhi_bond Is there a file named requirements.txt in the top directory of your project?
@ Dimitris I doesn't have django installed when I used pip freeze command, This seems that I have to install Django. Thanks buddy :)
No Alain, I didn't create requirements.txt file while uploading that project from my another PC. Anyways Thanks :)
|
5

If you have several python on your machine, for example,python2.7, python3.4, python3.6, it is import to figure out which version the python really reference to, and more over, which version does pip reference to.

The same problem got in my way after I installed the let's encrypt when I run the following command.

(python3 manage.py runserver 0:8000 &) 

I inspected the python version and found that python3, python3.4, python3.6, python3.4m were all available.

I just change python3 to python3.6 and solved the problem.

(python3.6 manage.py runserver 0:8000 &) 

So, this is probably a version mismatching problem if it is OK for a long time and crashes down suddenly.

Comments

4

I'm guessing you also upload the virtual environment from your other pc. And you hope that only activating that will work, bzz.

It's not recommended to upload the virtualenv files to your git repository, as @Alain says it's a good practice to have a requirements.txt file containing the project dependencies. You can use pip freeze > requirements.txt (when the environment is activated) to generate the project requirements file.

By doing so, when you clone the repository from another computer, you need to create a new virtualenv by issuing the command:

virtualenv nameofenv 

then activate it:

source nameofenv/bin/activate 

and finally, use the requirements file to install the requirements for your project using:

pip install -r requirements.txt 

Comments

1

I had installed Django 2 via pip3 install Django, but I was running python manage.py runserver instead of python3 manage.py runserver. Django 2 only works with python 3+.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.