How can I set environment variable WORKON_HOME for virtualenvwrapper-win - python

I am trying to setup virtualenvwrapper-win on a usb drive as part of a python development environment. In https://github.com/davidmarble/virtualenvwrapper-win it states:
Optional: Add an environment variable WORKON_HOME to specify the path to store environments. By default, this is %USERPROFILE%\Envs.
but doesn't explain how to do this. Could someone help with this?
My system python is at:
/f/python27/python

AS lguananut said, you are asking how to set an environment variable.
Take Win7 as example,
execute command "sysdm.cpl"
Go to Advanced tab, open "Enviroment Variables..."
New a system variable
In pop window, set variable name = WORKON_HOME, value = [your virtual environments root]
relaunch one command prompt, all commands like mkvirtualenv, workon will works.

If you prefer Powershell like me, you could also try the following code.
[Environment]::SetEnvironmentVariable("WORKON_HOME", "C:\Venv", "User")
[Environment]::SetEnvironmentVariable("PROJECT_HOME", "C:\Project", "User")
It will create two environment variables for your current user:
WORKON_HOME
PROJECT_HOME
Paste the sample code in PowerShell window:
[Environment]::SetEnvironmentVariable("WORKON_HOME", "<Your-Virtual-Environment-Root>", "User")
[Environment]::SetEnvironmentVariable("PROJECT_HOME", "<Your-Project-Root>", "User")
It will populate the variables like
Hope it helps.

Related

Is there a way to automatically load environment variables when activating venv?

I am using python venv to create virtual environments. But, since I am working with several projects with different virtual environments, I don't want to manualy set environment variables every time I switch to a different project.
Is there a way to set venv environment variables automatically when activating the venv?
What is the best practice for this problem?
A good practice is to use dotenv. You can load your environment by placing your environment variables into a file named .env, and whenever you would like to load an environment, just use the lines:
from dotenv import load_dotenv
load_dotenv()
This has the nicety that it only exists within the scope of you running a single script, since it essentially works like calling os.environ['variable'] = 'value' a number of times.
Activating a virtual environment is nothing more than sourcing a shell script. You can edit that script to set whatever variables you like. You will probably also want to edit the definition of deactivate to clear or roll back whatever changes you made to the environment.
you need to write a bash scirpt (in case you are using bash shell), where you specified a particular command which will activate the project python environment and add the project specific envrionment variable in the system environment. and remove the environment variable when you exit the project python environment.
but i don't this is good/correct way to do things. #mz solution will be correct, where you define a .env file and define env variable in it. and use load_env to read the env variable when project runs
This concept is based on Two Scoops of Django. I have implemented it using venv.
Open the Windows PowerShell Script in your virtual environment generated by venv.
The script is located at venv/Scripts/Activate.ps1
At the bottom of the file, you will see this line of code:
$env:VIRTUAL_ENV = $VenvDir
Below that code, enter your environment variable as follow:
$env:VARIABLE_NAME = 'variable_value'
Same concept goes if you are using the Command Prompt to activate the environment, you will need to place environment variables in venv/Scripts/activate.bat

Setting pipenv environment variable in venv

I am trying to set up a Python virtual environment with pipenv.
In VS Code, started in a fresh directory of Windows 10 OS, after doing:
python -m venv work_env in the Terminal
Noting the path of the Script folder within work_env
Doing Ctrl-Shift-P and Preferences: Open Workspace Settings
In settings.json, adding:
environment path of Step 2 to "python.pythonPath":
folder names to prevent pylance yellow squigglies to "python.analysis.extraPaths":
Doing a pipenv install in the Terminal
Updating package names in Pipfile
Doing pipenv update in the Terminal
... There comes a Courtesy Notice: Pipenv found itself running within a virtual environment, ... You can set PIPENV_VERBOSITY=-1 to suppress this warning.
In what folder / file / configuration do I set this PIPENV_VERBOSITY=-1 ?
According to the information you provided, I reproduced the problem you described:
The reason is as this 'Courtesy Notice', "pipenv install" is to create a virtual environment for the current project, and we have created a virtual environment "work_env", so it prompts us to ignore this environment or
Suppress this warning.
We can directly enter the command "set PIPENV_VERBOSITY=-1" in the console to suppress this warning:
After:
The pipenv version I use is 2020.11.4.
Update:
Entering the command set PIPENV_VERBOSITY=-1 didn't work for me.
So I added PIPENV_VERBOSITY variable and set the value to -1 in the environment variable window
Setting Environment Variables:
Right-click on My Computer or This PC and select Properties.
Go to the Advanced system settings or Go to the Advanced tab in the System Properties window
Click the Environment Variables button. The Environment Variables dialog opens.
Click the New button under either User variables or System variables, depending on whether you want to set the variable for the current user or all users. To set environment variables for all users, you need to have administrator privileges.
In the Variable name field, enter the name of the environment variable you want to set. For a list of the environment variables that Nuke understands, see Nuke Environment Variables.
In the Variable value field, enter the value for the variable. The value can be a directory path, for example.
Click OK.

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.

How to display conda's environment in zsh (not using oh-my-zsh)?

I use zsh but not equipted with oh-my-zsh, since I cloned many git repos on my machine and oh-my-zsh's default theme or 'josh' theme makes it very slow, for cd and ls commands, etc.
Thus I delete oh-my-zsh and only use zsh's default recommended config, except adding some alias and path variables.
Now comes the question: I manage my python environment by conda, and no current enviroment name displayed in my prompt.
I have executed the command:
conda init zsh
And re-open my terminal (default shell is zsh), still not display "(base)" or "(py36)" stuffs.
How can I tell my terminal to show that stuff? The git branch does not matters for my work, but the current python version really matters, since DeepLearning related tools use many different python versions.
For those users with oh-my-zsh
I add anaconda element in ~/.zshrc config file
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(anaconda ...ENVS)
and the conda env shows up
If you're not using oh-my-zsh and have not copy-pasted the relevant sections from there, then the correct answer is that when you do conda init zsh, the environment variable CONDA_PROMPT_MODIFIER is introduced. You want to add that to your PROMPT. You should do so as part of the precmd function, so that the an updated value of the environment variable is shown every time a prompt is printed.
This was caused due to this line in default zsh's config:
prompt adam1
Once delete that line, re-open terminal, conda environment are shown.
You can try,
# init your shell first
conda init zsh
source ~/.zshrc # refresh your shell
source activate base # (optional, on linux) activate base env

How to get path to virtualenv in Windows?

First of all, i'm using bash in windows.
I'm trying to write the correct paths to my virtualenv in VSCode but I must be doing something wrong
Any help will be really appreciated! thank you guys!
"python.pythonPath": "C\\Users\\Angel\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\\LocalState\\rootfs\\home\\aquesada\\.local\\share\\virtualenvs\\videoclub_django_new-coDPKRdg\\bin\\python",
"python.linting.pylintPath": "C\\Users\\Angel\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\\LocalState\\rootfs\\home\\aquesada\\.local\\share\\virtualenvs\\videoclub_django_new-coDPKRdg\\bin\\pylint",
"python.venvPath": "C:\\Users\\Angel\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\\LocalState\\rootfs\\home\\aquesada\\.local\\share\\virtualenvs"
virtual env as environmental variable
A virtual environment will set an environmental variable.
In Windows, you should be able to view that variable via echo %VIRTUAL_ENV%
In your Python code, you can grab that env var via os.environ['VIRTUAL_ENV']
VS Code config
python.venvPath should work. A few more things for you to try:
adjust path formatting: a single backslash as delimiter should work; AFAIK Windows only requires two consecutive backslashes at the start of the path
restart VS Code after updating configuration
use the Command Palette to select the Python interpreter from your virtual environment

Categories

Resources