ImportError: Couldn't import Django

前端 未结 22 1804
无人共我
无人共我 2020-12-05 01:59

I\'ve already configured virtualenv in pycharm, when using the python manage.py command, this is error shown:

E:\\video course\\Python\\code\\web_worker\\MxO         


        
相关标签:
22条回答
  • 2020-12-05 02:49

    You can use python3 to run file, if you don't want to use virtualenv.python3 manage.py runserver

    To install python3 look at this page

    0 讨论(0)
  • 2020-12-05 02:51

    I solved this problem in a completely different way.

    Package installer = Conda (Miniconda)
    List of available envs = base, djenv(Django environment created for keeping project related modules).
    

    When I was using the command line to activate the djenv using conda activate djenv, the base environment was already activated. I did not notice that and when djenv was activated, (djenv) was being displayed at the beginning of the prompt on the command line. When i tired executing , python manage.py migrate, this happened.
    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?

    I deactivated the current environment, i.e conda deactivate. This deactivated djenv. Then, i deactivated the base environment.
    After that, I again activated djenv. And the command worked like a charm!!

    If someone is facing a similar issue, I hope you should consider trying this as well. Maybe it helps.

    0 讨论(0)
  • 2020-12-05 02:52

    In case you have virtual env activated, django installed, django-admin --version prints the valid version - check if there is no circular import in the file you are executing.

    0 讨论(0)
  • 2020-12-05 02:52

    Looks like you have not activated your virtualenv when using the runserver command.

    Windows: <virtualenv dir>\Scripts\activate.bat

    Linux: source <virtualenv dir>\bin\activate

    You should see (name of virtualenv) as a prefix to your current directory:

    (virtualenv) E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
    
    0 讨论(0)
  • 2020-12-05 02:53

    When you install Django on your computer all things go fine but when you install a Virtual environment it gets separated from all things. You will know it's importance when you will make a final project and deploy it to any cloud or hosting.

    Just reinstall Django in the virtual environment and baam:

    pip install Django
    

    and then just run the command for testing:

    python manage.py runsever
    

    and you are all done.

    0 讨论(0)
  • 2020-12-05 02:58

    windows :

    1. (virtualenv dir)\Scripts\activate # this step to activate virtualenv

    2. you should be in the dir of (project name)

    3. python manage.py runserver

    0 讨论(0)
提交回复
热议问题