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.
Related
I have some noob level virtual environment questions. I've been using virtual environments a little but still have a few questions.
I have created and activated an env which is my main working environment as follows:
virtualenv env
source /path/to/environment/env/bin/activate
Having activated this, I can now see I am in the environment as I have (env) visible on command line.
My first question is, do I need to run the activate command each time I open a terminal session? And therefore each time I turn my laptop on, etc.?
Further, I want to create another environment that runs on an earlier version of python for testing purposes. I was intending to do it as follows:
virtualenv --python=python2.7 env-py2
source /path/to/new/environment/env-py2/bin/activate
Can these virtualenvs be switched easily? So can I activate env-py2 and then easily jump back to activate env again? Or is there an inbetween step required?
Apologies for the very basic questions but I was struggling to find high level information.
Yes you need to run activate command i.e. source each time you open a terminal session.
Switching between two virtual environment is easy. You can run deactivate command and source the other virtual environment.
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.
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
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)
I am having a bit of difficulty reinitiating my virtual environment on my preprod server on command line. I think when I quit out of my preprod server the other day, I also quit out of my virtual environment, because now it doesn't say (env) in front of the preprod prompt:
Before: (env) SAS#preprod:~$
Now: SAS#preprod:~$
I am logged into my preprod server now, and I have a virtual_environment_command.txt file, and a config file accessible. I just don't know how to activate either of those documents so I can utilize my virtual environment for testing the code for an api document I'm working on. Thanks!
You have to find the directory where the virtualenv was created. e.g. when you created it using virtualenv env it would create a directory called env and you can activate it using:
source env/bin/activate
Thank you so much for your responses! They were very helpful! I also ended up clicking the up arrow enough times to figure out what I typed in before to reactivate the venv a few days ago (yes it was a lot of clicking, lol) and found that the following command also proved to fix the problem:
SAS#preprod:~$$(cat virtual_environment_command.txt )
And that extra $ is not a typo, it goes the prompt and then $(cat virtual_environment_command.txt)
Thank you again!