简体   繁体   中英

Import Error When Running Django From Windows

Trying to run Django (version 2.1) from Window's Python 3.10.6. I received an Import Error from manage.py :

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?`

Have tried multiple things, with no luck:

  1. Installed Django on virtualenv through pip:
PS C:\Users\nickg\vidly6> newenv\Scripts\activate
(newenv) PS C:\Users\nickg\vidly6> newenv install django==2.1
(newenv) PS C:\Users\nickg\vidly6> django-admin startproject vidly6 .
(newenv) PS C:\Users\nickg\vidly6> pip install virtualenv
PS C:\Users\nickg\vidly9> code .
  1. Installed Django on pipenv:
PS C:\Users\nickg\vidly6> pipenv install django==2.1
PS C:\Users\nickg\vidly6> pipenv shell
  1. Used instructions from Django's site (created vidly9 file here so I could apply it to a different file):
PS C:\Users\nickg\vidly9> vidly9-env\Scripts\activate.bat
PS C:\Users\nickg\vidly9> python -m pip install django==2.1
PS C:\Users\nickg\vidly9> pipenv shell
PS C:\Users\nickg\vidly9> django-admin startproject vidly9 .
PS C:\Users\nickg\vidly9> code .

Yesterday, when going through Django's steps, I received a warning that the script was in the directory, but not in path:

C:\Users\nickg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts

So I went to System Properties -> Environmental Variables -> User Variables. It was there that in the “Path” variable, and added it as a value. Didn't work either.

In your first example, you've already created a virtual environment called 'newenv'. There's not really a need to use virtualenv after the fact, as it's a tool to create virtual environments. Pipenv is also a way of handling virtual enviroments, but it may be useful to go through the normal steps first, so you know what they make easier.

Usually the order of setting up django on a virtual env will go something like this:

#create a virtual environment so we can add all kinds of modules etc withouth breaking our default environment. 
# c:\dir could be any directory path we want to start in.
# myvenv is the name of our new virtual directory
c:\dir python -m venv myvenv 
#activate that virtual enironment and start it up - this puts us inside the new env
c:\dir myvenv\Scripts\activate
# now we are in the virtual environment (we can tell by the venv's name in brackets)
# we can install the latest pip
(myvenv) ~$ python -m pip install --upgrade pip
# With pip we can install django, specifying the version
(myvenv) ~$python -m pip install Django==2.1
# we can check that this worked
(myvenv) ~$ python -m django --version
# Now, still in the virtual env, we create our project
(myvenv) ~$ django-admin startproject vidly6

Don't forget that, if you leave the venv, you will need to re-activate it to add modules or run manage.py commands and also that you can tell you are in a venv by the (venvname) in brackets at your command prompt. It looks like you are using vscode, so, once created, you can activate your venv with a key combination - more details here: https://code.visualstudio.com/docs/python/environments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM