![](/img/trans.png)
[英]Import Error When Switchin from Python 2.7 to Python 3.4 on Heroku running django-oscar on django 1.7.6
[英]Import Error When Running Django From Windows
嘗試從 Window 的 Python 3.10.6 運行 Django(版本 2.1)。 我收到了來自manage.py
的Import Error
:
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?`
嘗試了多種方法,但沒有運氣:
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 .
PS C:\Users\nickg\vidly6> pipenv install django==2.1
PS C:\Users\nickg\vidly6> pipenv shell
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 .
昨天,在執行 Django 的步驟時,我收到一個警告,腳本在目錄中,但不在路徑中:
C:\Users\nickg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts
所以我去了系統屬性-> 環境變量-> 用戶變量。 它就在“路徑”變量中,並將其作為值添加。 也沒有用。
在您的第一個示例中,您已經創建了一個名為“newenv”的虛擬環境。 事實上,實際上不需要使用 virtualenv,因為它是創建虛擬環境的工具。 Pipenv 也是一種處理虛擬環境的方法,但它可能對 go 有用,首先通過正常步驟,所以你知道它們使什么變得更容易。
通常在虛擬環境上設置 django 的順序將是 go 如下所示:
#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
不要忘記,如果您離開 venv,您將需要重新激活它以添加模塊或運行 manage.py 命令,並且您還可以通過命令括號中的 (venvname) 告訴您您在 venv 中迅速的。 看起來您正在使用 vscode,因此,一旦創建,您可以使用組合鍵激活您的 venv - 更多詳細信息: https://code.visualstudio.com/docs/python/environments
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.