How do I fix Django changes made outside of my virtual environment? - python

I am new to Django and new to the virtualenv scene.
I have been working on a Django project for a couple of days and completely forgot to activate my venv on my second day.
I am using this tutorial to guide me and I have been working from within my activated environment up until this point....
Is it enough to just re-run manage.py after activating my venv?

Activating a virtual environment
Before you can start installing or using packages in your virtual environment you’ll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s PATH.
On macOS and Linux:
source env/bin/activate
On Windows:
.\env\Scripts\activate
You can confirm you’re in the virtual environment by checking the location of your Python interpreter, it should point to the env directory.
On macOS and Linux:
which python
.../env/bin/python
On Windows:
where python
.../env/bin/python.exe
Leaving the virtual environment
If you want to switch projects or otherwise leave your virtual environment, simply run:
deactivate
If you want to re-enter the virtual environment just follow the same instructions above about activating a virtual environment. There’s no need to re-create the virtual environment.
For Django
After active your virtual environment you need to run Django development server. To run the Django Development server go to manage.py directory.
And run the following command:
python manage.py runserver
And your Django Development server is running at http://127.0.0.1:8000/

Related

How to configure lsp-mode to run pylsp installed in a pipenv virtual environment?

I have tried to set lsp-pylsp-server-command to pipenv run pylsp and also to the path of pylsp in the virtual environment in .dir-locals.el. Both ways did not work because the language server seems to be started before the local variables are assigned.

how to visualize the python venv name in MacOs zsh prompt

I am just starting to learn django and I am facing the set-up phase. In particular I'd like to ask how to recognize if the virtual environment is activated or not. I know that I can use the command pip freeze but in all the tutorial that I am following, when the venv is activated, I can see the venv name in brackets in the terminal command line.
I can correctly activate the venv with the source command and check via the pip freeze command but I have no indication in the command line.
I am on a Mac/OS(M1) and using python3
thank you
To have visual information about the virtualenv on the command line you need to change the shell config to show it. It's not related to python or django itself.
It will depend on the shell that you are using, but assuming the default shell on mac you can check this question virtualenv name not show in zsh prompt
From venv docs.
When a virtual environment is active, the VIRTUAL_ENV environment variable is set to the path of the virtual environment. This can be used to check if one is running inside a virtual environment.
So you should be able to test it with:
import os
os.getenv('VIRTUAL_ENV') is not None
Yes, when the virtual environment is activated, it shows in the terminal as a prefix like
(env) yourname~Desktop/workspace-folder>
And that is enough to know it is activated, and you are using it.
Since you are using Python 3, you can create your virtual environment as follows in the same directory of your project, open terminal or iTerm and run this comman
python3 -m venv env
After few seconds, a folder env will be created that is your virtual environment. Note you can name your virtual environment as you wish, maybe like python3 -m venv djang-app
After you have created the virtual environment, you can now activate it like
source env/bin/activate
Again, if you created your virtual environment using lets say django-app
python3 -m venv django-app
You can activate it like this
source django-app/bin/activate
and you will see your terminal is prefixed as follows
(django-app) yourname~Desktop/workspace-folder>
To deactivate the virtual environment, you simply run this command deactivate in the terminal with active virtual environment.
You can learn more about python3 venv from here.

Navigating to Pycharm Virtual Environment

I created a new Python project and a virtual environment using Pycharm, however I am unable to navigate to the virtual environment using Ubuntu terminal. Could anyone share how I could enter the virtual environment through the Ubuntu terminal and run python files with the packages available in that specific environment.
My pycharm terminal shows this by default:
(conditional_slim_gan) arsh#Arsh:~/Desktop/machine learning/Outlier_dataset$
To activate the created virtual environment, run the following command in ubuntu terminal:
source /Desktop/machine learning/Outlier_dataset/bin/activate

Virtualenv Issue: Django Project

I had created and activated virtualenv and worked on my Django Project.
Now scene is that i've Reinstall my Windows 10. Now when i tried to enter my env e.g. workon DFMS
I face following error...
'workon' is not recognized as an internal or external command,operable program or batch file.
Or When i tried to run server using python manage.py runserver i face following issue...
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?
My Question is how to activate that old virtualenv for that project? and how to resolve this problem?
Thank You.
You could create a virtual environment with virtualenv venv and start it with venv/bin/activate.
make sure added the directory of your project into environment variable of your computer
you need to run Command prompt as Administrator.

When does venv need activation?

When using Pycharm, I make my virtual environment when I m creating a new project and then never have to think about activating it or anything. It works just fine.
When I m using the terminal on my Mac OS, I need to create the virtual environment and then also activate it.
I also have to activate it for VS Code.
How do I know when I need to activate my virtual environment? Thanks.
To activate a virtual environment from the terminal, you need to source a file that the venv module created. This file typically will be in the bin directory of your project (ej. my_project/folder/bin) and will be named activate.
So to activate the environment in your shell you would run the command
source my_project/folder/bin/activate
You should know if you're already IN the virtual environment, when you see the project's name parsed to some part of your shell prompt. If you're already into your virtual environment, you can leave it with the deactivate command.

Categories

Resources