Resolving this I installed Virtual Environment Wrapper which keeps all environments in one place and provides commands for creating and activating environments easily. With this , activation doesn't depend on locating the right activate script , by just using workon command
and the environment name in the terminal your environment is activated.
pip install virtualenvwrapper-win #for Windows users
pip install virtualenvwrapper #for Mac Os/linux Users
After installing our Virtualenvwrapper we need to now create our virtual environment using the mkvirtualenv command
.
mkvirtualenv dsargon
dsargon here is the name of my virtual environment.. you can name it anyhow you want.
Change Directory to your Project folder to install Django and other packages needed.
cd desktop \Udjango
pip install django
To install django in your project folder.
Confirm if your installed python and Django is available in your virtual environment by running the command below.
python
import django
exit()
Now we can confirm python is accessible in our environment created and as well we can import Django package.
Lets launch our visual studio code editor, change directory to our Project folder and run our project by using the runserver command
.
Python manage.py runserver
After running the above command if your Django project launches successfully Hurrayyyy !!! but if not it means you are in a different environment and that Django cant launch your project as dependencies and packages in virtual environments varies per projects. To correct this he have to let Django know the specific environment we want our project to run in using the “workon Command” .Now lets Change Directory to our project folder and deactivate the running virtual environment which in this case is the “trial” and activate the dsargon virtual environment using the workon command
deactivate #deactivates Current running virtual environment
workon dsargon
dsargon as mentioned earlier is the name given to our virtual environment we created earlier after installing our virtualenvwrapper.
This ensures that vscode python parser uses the right environment when checking for package dependencies in the project.
Alternatively you can switch the virtual environment from the bottom right corner of your vs code editor in my case it’s the (“trial":venv)
And select the right virtual environment for our project which is dsargon in this case.
Now 'runserver' after selecting the right virtual environment when the Django project is launched it should be able to run successfully all things being equal.
Top comments (0)