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

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]

Related

I activate venv in vscode but it doesnt appear in terminal

I have no idea whats going on but I activated venv by using Scripts/activate and it doesnt working yet, the (venv) isnt appearing
please someone could help me? I tried everything I could find lol
It looks like you are using Git Bash or a similar bash-like system for Windows. You need to run Scripts/activate.sh in this environment.
For PowerShell, use Scripts/activate.ps1, and for cmd use Scripts/activate.bat.
What terminal are you using? Please create a new powershell or cmd terminal. Also you should not open the folder of the virtual environment as a workspace. Below are the correct steps.
Create a new folder as a workspace, then open this folder in vscode
Create a new terminal using the following command to create a virtual environment
python -m venv .venv
Use the following command to activate the environment after creation
.venv\scripts\activate
Another way is to select the interpreter of the virtual environment in the Select Interpreter panel after creating the environment
And then the new terminal will automatically activate the environment
You can read creating-environments and venv.

AppInstallerRedirector terminal

I use python 3.7.4 on my system.
I tried creating a virtual environment on VS code for a project in the cmd terminal, but as soon as I enter the command python3 -m venv myEnv the terminal name changes from cmd to AppInstallerPythonRedirector and nothing happens.
I want help figuring out why this is happening and how to fix the issue and create a new environment for my project
The command python3 -m is for macOS/Linux. If you are using Windows, it should be py -3. Steps to create and activate the virtual environment for windows:
Create the virtual environment:
py -3 -m venv .venv
Note: When you create a new virtual environment, you should be prompted by VS Code to set it as the default for your workspace folder. If selected, the environment will automatically be activated when you open a new terminal.
Active the environment:
.venv\scripts\activate
If the activate command generates the message "Activate.ps1 is not digitally signed.
You cannot run this script on the current system.", then you need to temporarily change the PowerShell execution policy to allow scripts to run (see About Execution Policies in the PowerShell documentation):
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
Select your new environment by Ctrl+Shif+P, Select Interpreter command from the Command Palette.
If you are macOS/Linux, just follow:
https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments

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.

virtualenv not working on windows cygwin

I have cygwin installed on a windows 7 machine. I'm trying to follow the instructions listed here:
https://www.tutorialspoint.com/flask/flask_environment.htm
Created a new project directory
cd into the new directory
Typed virtualenv venv, and it created the new virtual dir successfully
This next step fails to work for me.
I'm trying to activate the virtual env but its not working.
"venv\scripts\activate"
When i type that nothing happens.
USER#PCTEST~/newproj
$ venv/Scripts/activate
USER#PCTEST ~/newproj
$ which python
/cygdrive/c/python27/python
Solution
Run activate script with source or . (dot space) command:
source ./venv/Scripts/activate
# Or
. ./venv/Scripts/activate
Explanation
source runs shell commands in your current shell environment, so venv works as expected. While running script by name starts new isolated shell environment, activates venv there and closes new shell environment returning to original one.

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