create shortcut to run python app using anaconda env - python

So downloaded an app from github, and set up a conda env to run it. I can run the app without problem when i open my anaconda prompt, activate my environment, and then run the app.py file. Now, i want to have a shortcut to do all these things. Usually when i do this with a non conda environment, i juste create a windows shortcut with the following parameters:
{location of my environment} {location of the python file i want to run}
Now, i'm still not very familiar with the conda envs, and when i do the same thing with a conda env, it dosen't work. I tried to put these lines in a batch file:
%windir%\System32\cmd.exe "/K" C:\Users\remic\Documents\storage\python\anaconda3\Scripts\activate.bat chunkmogrify
cd C:/Users/remic/Documents/storage/python/projects/2mp/chunkmogrify
python app.py
the first line is supposed to activate the chunkmogrify conda environment (this line works)
the second navigate to the location of the app.py file
and the third is supposed to run the app.py file.
The issue is that only the first line seems to work because when i run my .bat file, i get this:
So the environment is activated, but the location hasn't changed and the app does not run.
Therefore I wanted to know what i did wrong, or if you had another idea on how to setup this kind of shortcuts.
The end goal is to have a shortcut so that i can run app.py like any other software, but from the source python code without having to build an executable for the app.

You can use 'conda run' instead of 'conda activate'. E.g.
<path_to_conda.exe> run -n <env_name> python <path_to_python>
So for you:
C:/Users/remic/Documents/storage/anaconda3/Scripts/conda.exe run -n chunkmogrify python C:/Users/remic/Documents/storage/python/projects/2mp/chunkmogrify/app.py

Related

Run python script from Task Manager using specific environment

I'm trying to run a python script through Task Manager from a specific virtual environment.
However, "activate.bat" only seems to exist in the root folder:
C:\Users\user\anaconda3\Scripts
but not in
C:\Users\user\anaconda3\envs\env_name\Scripts
I've tried using the following batch file as the Task Manager Program/Script but doesn't work (nothing happens).
Any ideas on how to call a specific env here? Thanks!
#echo off
cd C:\Users\*user*\Documents\folderName\Scripts
call C:\Users\*user*\anaconda3\Scripts\activate.bat env_name
python test.py
Generally, conda run should be preferred for programmatic execution within an environment. The conda activate command is intended for interactive use in a shell. That is, something like
#echo off
cd C:\Users\*user*\Documents\folderName\Scripts
conda run -n env_name python test.py
I'm not on Windows, but you may need conda.exe or a full path to the Conda executable instead of simply conda.

Activate virtual environment and install requirements in the first setup automatically - Python, Windows

I want to write a first_setup.py script, where users can run it for the first time and the script will do the entire setup automatically, namely it will create a virtual environment, activate it, and install the requirements with pip. Users can then start to use the program without worrying about any dependency issue.
Therefore, I used venv together with subprocess library. My Python version is 3.7.5. With the following command, it creates the virtual environment in the working directory:
env_name = ".venv"
subprocess.run(["python", "-m", "venv", env_name])
However, activation doesn't work. I tried to activate it in various ways, for example:
subprocess.run([f"{venv_name}\\Scripts\\Activate.ps1"], shell=True)
This just opens Activate.ps1 in Windows Text Editor like a .txt file (?). I also thought to add .../Scripts/python.exe to the PATH variable, but it didn't work actually.
Furthermore, when the venv created by the script, VS Code pops up a message saying a venv detected, do you want to use it? If I click on Yes, then it changes my interpreter to venv, which is exactly what I want to do, but automatically with the first_setup.py script.
How can I proceed?
Does this first_setup script need to be Python?
The problem is that the activate script sets environment variables for the shell, which is why it’s usually run with ‘source’.
try using bash file:
python3 -m venv venv
source venv/bin/activate
pip install your_library

pytest githook on Windows with Anaconda

I have the following setup:
Windows 10
python installed via Anaconda
Virtual environment setup via Anaconda for running and testing my project with pytest
git version control via MINGW
Now I'd like to set a githook that runs all my tests before I push. I have the following problem: I can't activate my virtual environment within the githook.
I tried to activate my anaconda env in the githook script but I can't get it to work. activate as command is not available and calling the whole path ../Anaconda3/Scripts/activate.bat does nothing.
I also tried to use python-githooks to configure the hook for me, but this doesn't seem to work in Windows (it can't read PWD from the environment...)
I'm gratefull for any suggestions.
The solution was to create a .bat-File at the root of the git repository, with:
call C:\...\Anaconda3\Scripts\activate.bat
call activate fs_env
pytest
and to call this file within the pre-push file in .git/hooks with:
./runtests.bat

Is adding Flask env vars to the virtualenv's activate script OK?

I'm working on my Flask project in a virtualenv. Every time I start a new terminal, I have to reinitialize these Flask environment variables:
export FLASK_APP="server.py"
export FLASK_DEBUG="1"
My goal is to not have to type them in manually.
I tried writing a Python script that set them, but couldn't make it work. I tried writing a shell script that set them, but Flask would raise an error that said my Python path was incorrect.
Finally, I tried adding the the env vars to the bottom of the virtualenv's activate script. It worked! The env vars are set and Flask runs as expected.
$ source venv/bin/activate
$ flask run
Is it OK to modify the activate script like this? This is just for development purposes.
Yes, setting environment variables in the virtualenv's activate script is fine for managing your development environment. It's described in Flask's docs. They're only active when the env is activated in the terminal, and you have to remember to add them if you create a new env, but there's nothing wrong with it.
With Flask 1.0, you can use dotenv files instead. Install python-dotenv:
pip install python-dotenv
Add a .flaskenv file:
FLASK_APP=server
And the flask command will automatically set them when running a command:
flask run
The advantage of this over messing with the venv is that you can commit this file so it applies anywhere you work on the code.
Modifying venv/bin/activate file is working for you because the environment variable is getting defined inside the virtual environment. When you're using python3 -m venv venv the environment variables are not present in the new virtual environment. Instead of modifying the activate file, you can instead make a shell script which:
Runs venv/bin/activate
Defines the environment variables
Runs the server
First, I tried writing a Python script that set them, but after research, I realized it was not possible(?).
You could use os.environ to do the same from within, but a shell script is better.

Activate anaconda and start a webserver from cmd script

I would like to activate Anaconda environment and start a Python server from cmd script.
Currently the script looks like this:
cd "C:\Users\usmazuc\AppData\Local\Continuum\Anaconda32\Scripts"
activate my_env
cd "C:\wb"
python web_server.py
Script works for the first part - it activates anaconda environment, but it doesn't execute any command which follows activate my_env.
Currently I run this script from cmd console, but in the long run I would like to run it as a Windows startup script.
The goal is to:
1) activate anaconda env
2) from within this environment, start the Python server
The whole process shouldn't be visible for the user. The server should be running in the background but the user should not be able to see it.
In a batch script you should use call activate my_env. See https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/call.mspx?mfr=true.
You can also just use the full path to the environment when you call Python, like C:\Users\usmazuc\AppData\Local\Continuum\Anaconda32\envs\my_env\python.exe web_server.py. If you do this, there is no need to use activate.

Categories