Conda environments and .BAT files - python

I am setting up calls to python (Anaconda distribution) via BAT files and the windows task scheduler.
I've now used environments for the first time and was trying to set a .bat file up as below:
activate [my_env]
python my_script.py
deactivate
Unfortunately it appears that the second command does not get executed.

Use the 'call' command when activating/deactivating the environment.
call activate [my_env]
python my_script.py
call conda deactivate
See https://github.com/conda/conda/issues/794

Are you sure you need a batch file? I think this should work.
cmd "/c activate [my_env] && python my_script.py && deactivate"
When I made a simple file containing
print("Hello")
Which I called myprint.py and ran
cmd "/c activate anaconda33 && python myprint.py && deactivate"
This worked for me. You could also put this in a one line batch file.

All activate does is put the environment in the front of the PATH. You can just call the absolute path to the python in the environment you want, like C:\Anaconda\python my-script.py.

Related

I can't get VScode to activate a virtual environment called ll_env [duplicate]

I created a virtualenv around my project, but when I try to activate it I cannot.
It might just be syntax or folder location, but I am stumped right now.
You can see below, I create the virtualenv and call it venv. Everything looks good, then I try to activate it by running source venv/bin/activate
I'm thinking it might just have to do with my system path, but not sure what to point it to (I do know how to edit the path). I'm on python 7 / windows os, virtual env 2.2.x
Processing dependencies for virtualenv
Finished processing dependencies for virtualenv
c:\testdjangoproj\mysite>virtualenv --no-site-packages venv
The --no-site-packages flag is deprecated; it is now the default behavior.
Using real prefix 'C:\\Program Files (x86)\\Python'
New python executable in venv\Scripts\python.exe
File venv\Lib\distutils\distutils.cfg exists with different content; not overwri
ting
Installing setuptools.................done.
Installing pip...................done.
c:\testdjangoproj\mysite>source venv/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.
c:\testdjangoproj\mysite>source venv/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.
c:\testdjangoproj\mysite>source mysite/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.
c:\testdjangoproj\mysite>
source is a shell command designed for users running on Linux (or any Posix, but whatever, not Windows).
On Windows, virtualenv creates a .bat/.ps1 file, so you should run venv\Scripts\activate instead (per the virtualenv documentation on the activate script).
Just run activate, without an extension, so the right file will get used regardless of whether you're using cmd.exe or PowerShell.
I had the same problem. I was using Python 2, Windows 10 and Git Bash. Turns out in Git Bash you need to use:
source venv/Scripts/activate
I was also facing the same issue in my Windows 10 machine.
What steps i tried were:
Go to andconda terminal
Step 1
pip3 install -U pip virtualenv
Step 2
virtualenv --system-site-packages -p python ./venv
or
virtualenv --system-site-packages -p python3 ./venv
Step 3
.\venv\activate
You can check it via spider tool in anaconda by typing import tensorflow as tf
For activation you can go to the venv your virtualenv directory
by cd venv.
Then on Windows, type dir (on unix, type ls).
You will get 5 folders include, Lib, Scripts, tcl and 60
Now type .\Scripts\activate to activate your virtualenv venv.
Your prompt will change to indicate that you are now operating within the virtual environment. It will look something like this (venv)user#host:~/venv$.
And your venv is activated now.
source command is officially for Unix operating systems family and you can't use it on windows basically. instead, you can use venv\Scripts\activate command to activate your virtual environment.
Ensure venv is there and just follow the commands below. It works in Windows 10.
Go to the path where you want your virtual enviroments to reside:
> cd <my_venv_path>
Create the virtual environment named "env":
> python -m venv env
Add the path to the git ignore file (optional):
> echo env/ >> .gitignore
Activate the virtual env:
> .\env\Scripts\activate
For windows, type "C:\Users\Sid\venv\FirstProject\Scripts\activate" in the terminal without quotes. Simply give the location of your Scripts folder in your project. So, the command will be location_of_the_Scripts_Folder\activate.
if you already cd your project type only in windows 10
Scripts/activate
That works for me:)
Tried several different commands until I came across:
source venv/Scripts/activate
This did it for me. Setup: Win 10, python 3.7, gitbash. Gitbash might be the culprit for not playing nice with other activate commands.
A small reminder, but I had my slashes the wrong way on Win10 cmd. According to python documentation the activate command is: C:\> <venv>\Scripts\activate.bat
When you're browsing directories it's e.g. cd .env/Scripts
So to create my venv I used python -m venv --copies .env and to activate .env\Scripts\activate.bat
Open your powershell as admin
Enter "Set-ExecutionPolicy RemoteSigned -Force
Run "gpedit.msc" and go to >Administrative Templates>Windows Components>Windows Powershell
Look for "Activate scripts execution" and set it on "Activated"
Set execution directive to "Allow All"
Apply
Refresh your env
I have a hell of a time using virtualenv on windows with git bash, I usually end up specifying the python binary explicitly.
If my environment is in say .env I'll call python via ./.env/Scripts/python.exe …, or in a shebang line #!./.env/Scripts/python.exe;
Both assuming your working directory contains your virtualenv (.env).
You can run the source command on cygwin terminal
If you’re using Windows, use the command "venv\Scripts\activate" (without the word source) to activate the virtual environment. If you’re using PowerShell, you might need to capitalize Activate.
open the folder with any gitbash console.
for example using visualCode and Gitbash console program:
1)Install Gitbash for windows
2) using VisualCode IDE, right click over the project open in terminal console option
3) on window console in Visualcode, looking for a Select->default shell and change it for Gitbash
4)now your project is open with bash console and right path, put source ./Scripts/activate
btw : . with blank space = source
For windows Microsoft Tech Support it might be a problem with Execution Policy Settings. To fix it, you should try executing
Set-ExecutionPolicy Unrestricted -Scope Process
Finally Worked on Windows 10 by using:
activate myvenv
where myvenv is the name the virtual environment I want to activate.
So basically, the command is:
activate <<your_venv_name>>
All the best!
Some people are having trouble with vscode i assume as all the above methods dont work.
Its simply because by default vscode uses powershell not cmd...
click on the little arrow beside it and select cmd and run the command.
if .\venv\Scripts\activate does not work neither and you find this error
\Activate.ps1 cannot be loaded because running scripts is disabled on this system
you can simple type set-executionpolicy remotesigned in powershell and the error must be gone.
powershell should run as administrator
If you see the 5 folders (Include,Lib,Scripts,tcl,pip-selfcheck) after using the virtualenv yourenvname command, change directory to Scripts folder in the cmd itself and simply use "activate" command.
If wants to open virtual environment on Windows then just remember one thing on giving path use backwards slash not forward.
This is right:
D:\xampp\htdocs\htmldemo\python-virtual-environment>env\Scripts\activate
This is wrong:
D:\xampp\htdocs\htmldemo\python-virtual-environment>env/Scripts/activate
The steps for activating virtualenv using Python3 on windows are:
python3 -m venv env
.\env\bin\activate
Use These it worked for meenv\Scripts\activate
For those of you crazy enough to have a custom DOS prompt, venv activation may fail, depending on your %PROMPT% environment variable content.
Say you have a DOS prompt defined as follows:
set PROMPT=$_$E[1;30;40m[$E[0;36;40m%computername% | %userdomain%\%username%$E[1;30;40m] $E[0;33;40m$M$_$E[35;40m$d$s$t$h$h$h$h$h$h$_$E[1;33;40m$p$_$E[1;30;40m$g $E[0;37;40m
(btw, it shows as follows, with some fancy colors:
[W10-MY_COMPUTER | W10-MY_COMPUTER\JOHNDOE]
15/09/2022 20:31
F:\
> echo "Hello World"
Hello World
[W10-MY_COMPUTER | W10-MY_COMPUTER\JOHNDOE]
15/09/2022 20:31
F:\
>
)
Then if you try to activate a venv :
.\venv\Scripts\activate.bat
it may fail with a rather unclear error that states:
Path not found
without further indications.
The trick is that activate.bat(/deactivate.bat) scripts try to save(/restore) the %PROMPT% variable with this syntax:
...
set _OLD_VIRTUAL_PROMPT=%PROMPT%
...
set PROMPT=(venv) %PROMPT%
...
It can be fixed with this syntax:
...
set "_OLD_VIRTUAL_PROMPT=%PROMPT%"
...
set "PROMPT=(venv) %PROMPT%"
...
now it shows:
[W10-MY_COMPUTER | W10-MY_COMPUTER\JOHNDOE]
15/09/2022 20:31
F:\
> venv\Scripts\activate.bat
(venv)
[W10-MY_COMPUTER | W10-MY_COMPUTER\JOHNDOE]
15/09/2022 20:31
F:\
>
Instead of using:
source ./venv/Scripts/activate
Use only:
./venv/Scripts/activate
Your Script will be activated.
If you are using windows OS then in Gitbash terminal use the following command $source venv/Scripts/activate. This will help you to enter the virtual environment.
Open your project using VS code editor .
Change the default shell in vs code terminal to git bash.
now your project is open with bash console and right path, put
"source venv\Scripts\activate" in Windows
Navigate to your virtualenv folder eg ..\project1_env>
Then type
source scripts/activate
eg ..\project1_env>source scripts/activate
The best way is, using backward slahes and using .bat at the end of activate
C:\Users>your_env_name\Scripts\activate.bat
:: location of bat file
::C:\Users\gaojia\Dropbox\Projects\free_return\venv\Scripts\activate.bat
:: location of the cmd bat file and the ipython notebook
::C:\Users\gaojia\Dropbox\Projects\free_return\scripts\pre_analysis
source ..\..\venv\Scripts\activate
PAUSE
jupyter nbconvert --to html --execute consumer_response_DID.ipynb
PAUSE
Above is my bat file through which I try to execute an ipython notebook. But the cmd window gives me nothing and shut down instantly, any suggestion why would this happen?

Activating virtualenv from batch file causes venv in new command prompt [duplicate]

I've done a fair bit of bash scripting, but very little batch scripting on Windows. I'm trying to activate a Python virtualenv, run a Python script, then deactivate the virtualenv when the script exits.
I've got a folder called env, which is my virtualenv, and a folder called work, which contains my scripts.
This is what I've got so far:
%~dp0env\Scripts\activate.bat
python %~dp0work\script.py
deactivate
However, when I run the script, it activates the virtualenv then stops. It does not get to the second line and run the Python script. Is there a way to "source" the activate script folder, so that the rest of the batch script can be run as if I'd called activate.bat from the command line?
I'd say you just need to prepend 'call' to your activate.bat invocation, to ensure that the current batch file is resumed after activate is executed:
call %~dp0env\Scripts\activate.bat
Consider doing the same for deactivate.bat. Furthermore, if you want to ensure that the current cmd.exe environment is not polluted by a call to your batch file, consider wrapping your commands in a setlocal/endlocal command pair.
I made a .lnk file that points to cmd /k "path/to the/script/activate.bat", and it works.
CMD parameters & options
I suppose you just want to perform the same commands in Windows as if expected in Linux Bash/shell. When I want to start a virtualenv I am actually in its top directory, and the Linux command would be "source bin/activate".
It is no problem to simulate this behaviour on Windows. Me personally, I've put a batch file named activate.bat somewhere on the PATH environment variable like this:
:: activate.bat
#echo off
REM source bin/activate
if "%1" == "bin/activate" (
if not EXIST "%CD%\Scripts\activate.bat" goto notfound
set WRAPEX=Scripts\activate.bat
) ELSE (
set WRAPEX=%*
)
call %WRAPEX%
goto :eof
:notfound
echo Cannot find the activate script -- aborting.
goto :eof

How can I activate another user's conda environment?

a colleague of mine has written a python script that I need to use, which is called within a shell script. It produces plots with matplotlib. However, when I try to run his script, it fails in matplotlib commands with "ImportError: No module named PyQt4". The python script is called within the shell script with a syntax like
./script.py
script.py begins with a line to specify the python exec to use from within his miniconda environment, like
#!/user/miniconda/envs/py27/bin/python
I think the problem is that the code uses the default PyQt on my system when I run this command. I tried running script.py with the python exec in his environment, but this gives the same error. This also occurs if I try to run the script on his computer when logged into my account. Is there a way that I can run this script as if I were my colleague within my account?
Have your colleague generate a yaml file with his environment dependencies, then create a copy of his environment on your computer to run the script.
# your coworker runs:
conda env export -n [name of his environment] > environ.yml
Once you get yaml file, you can run
conda env create -f environ.yml
to copy the environment. From there, activate it and run the script
# on Windows
activate [environment name]
python ./script.py
# on *nix
source activate [environment name]
python ./script.py

Is there a single line way to run a command in a Python venv?

I have a command that only runs correctly inside a Python virtual environment I've configured (as intended). I know that I can run the command as
$ cmd args
once I've activated the venv. But (due to the constraints of the tool I'm using) I need to activate run (and deactivate?) in one line: something equivalent to running
$ activate_somehow cmd args
outside the command line.
Is there a way to do this?
You can generally run something in a virtual environment simply by using a fully qualified path to the script. For example, if I have:
virtualenv .venv
Then I can install something into that virtual environment without activating it by running:
.venv/bin/pip install foo
This should be true for anything installed using standard Python mechanisms.
After looking into the generated bin/activate script, it seems like the only thing relevant to python is the VIRTUAL_ENV variable, so this should be enough to get going:
$ env VIRTUAL_ENV=path/to/venv python ...
Note that the python executable in the bin directory of target environment is just a symlink to globally installed interpreter, which does nothing other that setting process executable path. Assuming the program does not make use of it, utilizing the main binary itself seems harmless. In case you have installed a package which in turn installs some executables, just specify the absolute path:
$ env VIRTUAL_ENV=path/to/venv path/to/venv/bin/executable
You can create a simple wrapper script which runs activate, executes your command, and then deactivates simply by exiting the script in which your environment was activated.
#!/bin/sh
. ${venv-./env}/bin/activate
"$#"
This lets you set the environment variable venv to the path of the environment you want to use, or else uses ./env if it is unset. Perhaps a better design would be to pass the env as the first parameter:
#!/bin/sh
. "$1"/bin/activate
shift
"$#"
Either way, save this somewhere in your PATH ($HOME/bin is a common choice for your private scripts) and give it executable permission.
I found venv-run which should do what you ask:
pip install venv-run
venv-run cmd args
Larsk's answer is probably cleaner, but this is another possible way.
Assuming you use UNIX and your user is user and you have a virtual environment in home (any) directory, ie /home/user/venv, you can make a script like:
#!/bin/sh
export VIRTUAL_ENV=/home/user/venv
export PATH=/home/user/venv/bin:$PATH
python3 "$#"
We can make this script executable (eg call it venv-python3 and do chmod +x venv-python3) and call it as such, or put it some place discoverable in PATH - let's say alongside python. Assuming you have sudo rights:
sudo cp venv-python3 /usr/bin/venv-python3
Then we can call that instead of the python callable. Since the variables are set within the script, explicit call on deactivate is not necessary at exit.
Example:
user#machine ~ % venv-python3 --help
This works for at least for virtualenv version 20.0.17 but if adopted, you should be keeping an eye on what variables bin/activate sets, if this ever changes.
Yes, you can execute the python file using a virtual environment in a single line of command on windows.
venv\Scripts\activate&&python fall_detector.py
I installed pgadmin4 in my home directory in a virtual environment called "pgadmin4".
I use fish shell and it runs perfectly fine with:
~/pgadmin4/bin/python3 ~/pgadmin4/lib/python3.10/site-packages/pgadmin4/pgAdmin4.py
Just in case this helps somebody.

Activate virtualenv and run .py script from .bat

I'd like to use Windows Task Scheduler to run a python script within a virtual environment. I'd like the Scheduler to run a .bat file that will
activate the virtualenv
run the script
These steps work together from the command line, and they work individually in a .bat, but I can't seem to get them to work together from the .bat. It seems the virtualenv is not fully activated when I try to execute the python script and confused as to why.
My .bat looks like this:
call workon venv
cd path/to/Python/proj
python -m script.py
I've tried adding timeouts immediately after the call to workon and tried moving the workon to seperate .bat called from my first file, but the other lines still execute before the virtualenv is activated. Any help is greatly appreciated!
You do not need to activate the virtual environment while running in .bat. All you need to do is to run the python.exe file in your virtual environment.
{path to virtual environment directory}/Scripts/python.exe path/to/your/file.py
In Windows Task Scheduler you can specify the path in which the command prompt will open. So all you need to do is when adding the action, use path to your python in the field Program/script, the name of the file to be run in Add arguments field, and the path to your file.py in Start in field.
P.S if you are reading or writing files in your python file, note that your path will be relative to the one you specify in your start in field in the Action window
You can use an ampersand & operator in a oneliner batch file.
call workon venv & cd path/to/Python/proj & python -m script.py
It will run each command after the other.
You can also double up the ampersand to make it a conditional operator. &&:
call workon venv && cd path/to/Python/proj && python -m script.py
Here the command will only run, if the previous command completed successfully, in other words ERRORLEVEL = 0
Just type
call .\venv\Scripts\activate.bat
in the .bat file and any command afterwards will see the venv activated
for the record call in a cmd pauses the execution of the current script, executes the called one and then resumes.
Create .bat file
write virtual environment activate script location and python file location as below use '&' operator to run two commands.
as below:
"E:\Call Allocation Engine\Development\development_env\Scripts\"activate & python run.py
https://i.stack.imgur.com/31Gkh.png
finally place this file in desired folder and run using cmd.
E:\Call Allocation Engine\Development\Optimisation\Scheduling>file_name.bat
this script will activate virtual environment and run your python code in that environment.
Another way to do this is to make a shortcut of the batch file and then change the "Start in" field.
After that remember to use the full paths in your batch file since it will be running from a difference location.
Edit activate.bat and place this line at the bottom:
python yourscript.py
Schedule the activate.bat itself and it will automatically run your script after the virtual environment activated.

Categories

Resources