Activate anaconda and start a webserver from cmd script - python

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.

Related

Is it necessary to run activate command after activating virtual environment using pipenv shell command?

As far as I know, for activating the virtual environment it's enought to just run pipenv shell
But in my case after running this command the parentheses at the beginning of the command prompt doesn't show up. Even after selecting the proper interpreter (the one inside venv folder)
To show the parentheses I should run activate command afterward.
So my question is: Is it important to show the project name in parentheses at the terminal prompt? Should I run the activate command after pipenv shell command every time?
I will appreciate if you describe the meaning of those parentheses. Tnx.
PS: I am using vscode in windows 10.
You do not need to activate the environment manually. When you select the python interpreter and open the terminal, vscode will activate it automatically for you.
The Python extension uses the selected environment for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a .py file open in the editor, and opening a terminal with the Terminal: Create New Terminal command. In the latter case, VS Code automatically activated the selected environment.
You can read document for more details.

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.

create shortcut to run python app using anaconda env

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

How do I run a python program in a virtual environment from windows command prompt?

I created a virtual environment for a Python program using PyCharm. I can run the program just fine straight from PyCharm terminal but when I try to run it from windows command prompt I get an error because some modules in the program are only installed in the virtual environment.
So my question is: how do I run a Python program that is in a virtual environment from the command prompt?
$ cd your_project
$ ./activate.bat
$ python helloworld.py
Explaination: virtualenv should create a .bat file in your project root directory. To run it, activate the virtual env first then run the python command.
I think you have to look after switching virtual environment on windows
may be this will help
[https://towardsdatascience.com/manage-your-python-virtual-environment-with-conda-a0d2934d5195][1]

How can I activate a Python virtual environment and launch a python script, in a bash script on Mac?

I am trying to create a bash script that will simply
Activate my venv
Launch a python script
that can be double clicked and executed on Mac.
My script is as follows:
#!/usr/bin/env bash
./macVenv/bin/activate
python main.py
I can run from command line just fine. When I double click though it complains it cannot find the python file.
Yes. The following script works on my machine.
source activate $1
python $2
I run it by typing ./script_name py36 python_file_name
where script_name is the name of the script with these 2 lines. py36 is the name of the virtual environment and python_file_name is the python script you want to run.

Categories