Python/Django: How to reactivate pipenv after exiting - python

Please Everybody. I have a little problem with pipenv.
For instance, I am work on a Django project and I decided to sleep. I had to shutdown my laptop for some reason. Then i woke up navigated to the project and I open it in VScode again. My question is how to I reactivate the pipenv environment again.
I mean something like source bin/activate if you are using virtualenv
I use pipenv shell but i want to be sure that is absolutely right.

pipenv —venv to get the location of the virtualenv
Then activate it like a normal virtualenv.

As a new user of both Django and Pipenv I recently ran into the same issue. Maybe this will help someone.
(for Mac m1 steps maybe different for you)
expose hidden folders inside the home directory. Shift + command + >
you should see a folder called " .local." follow it through...
.local > share, virtualenvs. The virtualenvs is what your looking for. It contains folders for all of pipenvs.
from the integrated terminal inside vscode I cd into whichever pipenv folder coorespondes to the project I'm working on then run:
source bin/activate.
the terminal should return with an indicator in parens ahead of user name confirming the shell is active again:
(Blog) user#user-MBP Blog %
cd back into project folder (the one containing manage.py) and run python manage.py runserver
should be good to go.

pipenv --venv => outputs the virtual environment directory (ie "/Users/td/.local/share/virtualenvs/data-infra-HlhPgy0O")
You can copy the output of the previous command and then run (on zsh):
source [vitrual_environment_directory]/bin/activate
(ie source /Users/td/.local/share/virtualenvs/data-infra-HlhPgy0O/bin/activate)

Related

activating virtual environment not working

On my server (digitalocean droplet), when I activate my virtual environment, it seems something is not working. I say the because:
It does not give me the virtual environment name inside paranthesis on the command line
When I run deactivate commend, it says deactivate: command not found
user#django-s-1vcpu-2gb-sfo3-01:~/example$source env/bin/activate
user#django-s-1vcpu-2gb-sfo3-01:~/example$
running source env/bin/activate should output the following command line:
(user) user#django-s-1vcpu-2gb-sfo3-01:~/example$
I did check which folder I am at and if the env virtual environment is shown under that folder. I also looked at this point, and when I go to python manage.py shell and type those lines of code, it says I am not in the virtualenv! So, why is that? and how to fix it without completely mess up my live production site?
I just recreated the virtual environment using virtualenv env. I was reluctant to do so fearing making more problems with my live production site, but it looks OK so far. I will update if this solution has unintended consequences down the road.

Python - Reactivating my virtualenv for django project

(Beginner) I am working with a virtualenv for a django project using my Macos terminal and Visual Studio Code.
Recently, my terminal disappeared where I was running all of my code and I do not know how to reenter my virtualenv or my project that I still have on Visual Studio Code. I have tried everything including:
source virtualenv/bin/activate
Or,
virtualenv/source/activate
Or,
cd my_project_name
and then trying to enter my virtualenv following this step (^) with the commands above.
Nothing is working and when I run "python manage.py runserver" it says there is no such file or directory. I am struggling and do not know how to continue my current project on django before getting back to my project and virtualenv.
Any help is greatly appreciated!
Visual studio code actually has a convenient way of keeping track of virtual environments. At the very bottom in the blue footer bar, click next to the refresh icon, and you should see whether it's available.
If it's really gone, don't sweat it. Virtual environments are completely expendable – it'll turn up. For now, just make another, and be sure you create it in your project's root directory this time.
You actually usually don't need to activate a venv with vscode. Just cd into the project root directory and open vscode with code .. The activated envionment will appear in that same spot in the footer.
Also, look into using a requirements.txt file, it'll save a lot of time if you keep misplacing the virtualenv.
And finally, it's best not to name your venv "virtualenv", name it something unique – otherwise it's guaranteed to confuse!

How to make sure that my django project is using virtual environment that i created for it?

I know that there is already a question similar to this,but i think the answer i wanted is not there.
I am new to django.i have created an virtual environment with virtualenv and a django project,but how can we know that my project is using the packages of virtual environment rather than using global packages??please give me some detailed answer.THANKS in advance.
you make sure you run /path/to/my_venv/bin/activate and your command shell prompt should look like (my_venv) C:\ > or (my_venv) /home/user$ or similar depending on your os
import sys
print(hasattr(sys, 'real_prefix') or sys.base_prefix != sys.prefix)
Try running above code. If False you are using global env and if True then virtual env.
One way is to use one of the IDEs to help you with that.
If you use PyCharm for example, it helps you with defining the virtual environment, and then when you open their terminal, it will be starting the venv for you.
https://www.jetbrains.com/help/pycharm/creating-and-running-your-first-django-project.html
If you run your commands from the command line/terminal, you need to activate the virtual environment first and then use the python from that venv.
https://developer.mozilla.org/en-US/docs/Python/Virtualenv
I usually create a venv folder inside the project root folder (next to the source folder), and I make sure it's ignored in the .gitignore file.

Activated Virtualenv in commands but the bracket is not showing in the directory [Python]

I am trying to learn django.
In VS code, I have installed pip and virtualenv.
I created a env folder by using virtualenv command.
$ virtualenv env
To activate virtual environment, I ran the command below.
$ source env/bin/activate
The result I was expecting to see was I see the little brackets in front of directory address as below:
dhkang#dhkang-Lenovo-IdeaPad-S145-15API ~/fastcampus/django
% (env)
However, I do not see any brackets to indicate that the virtual environment is being activated.
dhkang#dhkang-Lenovo-IdeaPad-S145-15API ~/fastcampus/django
% source env/bin/activate
However, I do not see (env) here. I don't know if the virtual environment is activated or not.
Does anyone know how to resolve to show the brackets when virtual environment is activated?
Ok if you are using builtin zsh in vscode, After you activated the virtualenv you should delete the terminal and start new one using the view tab...
Delete terniaml using icon in right top of the terminal. (Use the trash icon)
Start new terminal..
View > Terminal
Now, I resolved my issue.
It was one of zsh settings that I could never imagine as I am fairly new to Linux.
What I did was trying out other shell types like bash. I tried bash as default terminal and the bracket indicating it's on virtual environment was showing.
In the end, I have installed oh-my-zsh and changed the theme 'agnoster'. Now, anywhere in terminal or VScode clearly show the brackets. With regards to oh-my-zsh, refer to the link https://github.com/ohmyzsh/ohmyzsh

django for python set up issues

I'm not familiar with using command prompt and having trouble at the start of the django for python set up process which i'm reading here:
https://docs.djangoproject.com/en/2.2/intro/tutorial01/
My problem appears to be the 'python' command doesn't work (please see my command prompt screen shot).
I tried adding it into 'System variables' in the 'Environment Variables' as shown in the other screen shot.
Is there something i have done incorrectly here?
Also although i can get the create the initial project the only place i can create it is in the 'c:\Python\Lib\site-packages\django\bin' folder because i have to be looking at this folder in order to run the django-admin.py file.
I think the second issue is related to the first issue though i.e. if the 'python' command worked i could just run 'python django-admin.py startproject mysite' while my directory path is set to where ever i want the project to be created.
I expect i could simply move the mysite folder to where i want it but i feel i need to understand why this all isn't working as expected before moving on.
Additional:
I've now tried creating a virtual environment as suggested in the comments.
Running 'pip install virtualenvwrapper-win' appears to have worked. Running 'mkvirtualenv myproject' has not worked. It says 'C:\Users\mogwa\Envs is not
a directory although i can view this path so it is actually there.
https://docs.djangoproject.com/en/2.2/howto/windows/
Create Python virtual envoirement (virtual env, venv). Wait a little.
for Linux/GNU:
python -m venv venv
for Windows: (If not python in windows paths)
py -m venv venv
Activate your new python envoirement
for Linux/GNU:
source venv/bin/activate
for Windows: (Option 1)
source venv/Scripts/activate
for Windows: (Option 2)
.\venv\Scripts\activate.bat
Install django over pip (You should see (venv))
pip install Django
Start new Django Project:
django-admin startproject projectname
For Start new Django App:
django-admin startapp appname

Categories

Resources