Activating a Virtual Environment when running .bat file using Windows Scheduler - python

Using pycharm, I finished my first python program.
I am attempting to run a bat file with Windows Scheduler.
The python script has lots of dependencies. They are available in the virtual environment I am using.
I tried activating my virtual environment using the activate.bat file in the directory of the venv.
The code of the scheduled bat file looked like this:
C:\Users\PavelWorkXPS\Anaconda3\envs\TestEnv\Lib\venv\scripts\nt\activate.bat "D:\Dropbox\Toolkit\Python\Unusual_Options_Activity-ToS\main.py" pause
When running this, Pycharm would launch because it is my default .py extension handler.
I modified the bat file to use the interpreter python.exe from within the virtual environment folder, hoping it would have access to the dependencies.
C:\Users\PavelWorkXPS\Anaconda3\envs\TestEnv\python.exe "D:\Dropbox\Toolkit\Python\Unusual_Options_Activity-ToS\main.py" pause
Output would tell me it still can't see the dependencies.
I used this earlier stackoverflow post for help, but it only explained the reason
why pycharm was opening by itself.

Conda was the reason for the problem. When I switched to doing it with a plain virtual environment, it stored all the downloaded repos in a folder inside of the folder with the script. Then I setup the bat file and all worked fine.

Related

Visual Code how can I activate the virtual environment automatically?

Maybe a simple question, but I cannot figure it out. I work within Visual Studio Code on a MacBook. Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.
In the terminal I see (.venv) (base) gaston#MacBook-Pro WebScraping %
When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.
My question is there a way to activate the virtual environment automatically when opening the directory?
VS Code Default Python Interpreter Path
You can do this in a number of ways. If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.
https://code.visualstudio.com/docs/python/environments
System Preferences->Settings->Python: Default Interpreter Path
You'll want to point it to your virtualenv Python installation, likely something like:
envFolder/bin/python3
VS Code Launch Configuration Python Interpreter
You can also set a specific launch configuration so that VSCode will still use your system Python instance by default; however, specific launch configurations will have the virtual environment specified.
https://code.visualstudio.com/docs/python/debugging
I did something within settings and now it works.
When I open VSC now it opens the directory and the right environment, just the way I wanted it.
Took me some while to figure what finally did the trick:
I opened settings Command + , typed env in the search box. Scrolled through the results until I saw:
Python > Terminal: Activate Env In Current Terminal
and checked the checkbox. That solved my problem.

How to get terminal to default open into my virtual environment?

I've created a virtual environment for Python and VSCode, but everytime I open up the terminal, rather than opening up to the work environment folder, it opens up to the folder where I keep my .py file.
Let me explain my steps.
I've created a Work folder, then a work environment folder inside of it. C:\Work and C:\Work\work_env
Next I've created a folder in C:\Work\myprojects to place my .py files in.
Now when I open up the terminal in VSCode, it runs the Activate.ps1 file and my work environment is ready to use but ONLY when I navigate into my work environment folder using cd in the terminal.
Is there a way to default it so that the terminal is open to the work environment right away? I followed all the steps in this short tutorial and this guy got his terminal to work that way but it's different for me. https://www.youtube.com/watch?v=Wuuiga0wKdQ
Thanks!
Add the following line to your settings.json:
"terminal.integrated.cwd": "DESIRED/PATH"
You can find more details here.

Run PyCharm project outside of PyCharm

I have a PyCharm project which uses a virtual env and its own site-packages and I want to be able to run it without needing to open PyCharm everytime.
My current naive solution is runing a batch file which launches the python in the venv and the main script python.exe ../../PythonFiles/Main.pyw. The issue with this is that the console will stay open as its running from a batch process.
You can activate your virtual python environment by this command
source activate yourenvname
then change the directory and go to source directory ../../PythonFiles/ and type
start pythonw Main.pyw
This will help to start the python script in the background & If you don't want to run in the background and keep terminal open remove pyw change it to py extension then run it.

Batch that starts python virtual environment and runs the virtual python in a project dir

I want to run each day a python script in a virtual environment by recording it in Windows Task Scheduler.
Hence, I need to write a batch that:
Opens a cmd.exe
Starts a virtual environment
Changes the directory to a python project directory
Invokes the virtual python
I wrote this .bat:
cmd /k "cd /d %USERPROFILE%\python_venv\venv1\Scripts & activate & cd /d %PROJECT_DIR%\src"
python main.py
Of course cmd /k does not terminate, and python main.py is not executed.
Sorry in advance if the answer already exists over the Internet but I was not able to find it or to modify a close answer to suit my needs.
[EDIT]
This question is a duplicate of Schedule [Virtualenv Dependent] Python Script with Windows Task Scheduler.
This post has also not been answered yet.
[EDIT 2]
Found an answer here: Run a python script in virtual environment from windows task scheduler
This might be completely irrelevant but I've had a great deal of success using PyCharm + virtual environments with python, you set your VM's file structure up as a project and just add the python environment that you'd like to use with it, it's relatively simple:
Requirements:
Virtual Env
Pycharm
Go to ssh to the virtual env and type which python
Add remote project interpreter (File > Default Settings > Project Interpreter (cog) add remote) and then type in/select the location for the remote python location
You'll need to set up your file system so that PyCharm can also open the project.
NOTE:
Do not turn off your virtual environment without saving your run configurations that will cause pycharm to see your run configurations as corrupt
There's a button on the top right that reads share enable this and your run configs will be saved to a .idea file and you'll have a lot less issues

virtualenv activation on windows

I have installed virtualenv 1.10 on a Windows 7 machine with Python 2.7 on it.
I have created a virtual env called TESTENV. It is located in C:\
If I run C:\TESTENV\Scripts\activate and then type python followed by:
import sys
print sys.prefix
it outputs C:\TESTENV, as exprected.
But if I run D:\virtualenv_test.py (which is a script containing the two lines of code I used above) it outputs C:\Python27.
I tried adding a shebang line to the script but that doesn't work on Windows.
Why is the script not running in the context of the virtual environment?
virtualenv doesn't change the default application that starts a file.
When you installed Python, the installation associated .py and .pyw files with appropriate C:\Python27\python.exe and C:\Python27\pythonw.exe applications. So when you run (in your cmd) only D:\virtualenv_test.py, cmd looks into the registry and finds the application that is associated with .py files and then runs the file with that application. In this case that is the previously installed C:\Python27\python.exe.
virtualenv activate only adds that particular virtual environments python interpreter at the start of the PATH environment variable, so when you type python in cmd you will get the appropriate python.exe executed.
So the solution to your problem is to run your script as:
python D:\virtualenv_test.py
After you activated the environment.

Categories

Resources