Visual Studio Pre-build event can't activate conda environment - python-3.x

I have some python scripts to execute before building. I have a batch file activating the environment and call the script. It runs fine on my laptop both in windows cmd and VS prebuild. But when I exported enviroment.yml and shared it with colleagues, they are facing the following error:
Error
Initially this also occurred in the cmd. It's solved by running conda init. But the same error occurred in VS and I don't know how to solve this.
The following is my .bat file
call conda activate myEnv
cd ..
if "%1"=="" goto aa
if "%1"=="bb" goto bb
if "%1"=="cc" goto cc
:aa
python ..\xx.py %aa
goto commonexit
:bb
python ..\xx.py %bb
goto commonexit
:cc
python ..\xx.py %cc
goto commonexit
:commonexit
call conda deactivate
exit
I have also tried to call conda activate directly in the prebuild event command line and the same error occurre. It worked on my laptop and not on my colleagues', and I can't recall doing anything special. We are all using VS 2022.

Related

`conda activate <environment>` on Spyder console not working

I have python 3.7.10, conda 4.12.0, and Spyder 5.0.5 installed on Windows 10, and I can see the list of environments with conda env list.
However, when I run conda activate <environment>, CommandNotFoundError appears:
In[1]: conda activate <env>
Note: you may need to restart the kernel to use updated packages.
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
[...]
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
I like to change the environment without running Anaconda navigator (or close Spyder every time I change the environment).
Is there a command that I can run on Spyder console to change the virtual environment? Is it also possible to run different environments on different Spyder consoles?
To change environments you will need to change the interpreter preference and restart the console or create a new one. To change the interpreter preference you can go to Tools > Preferences > Python Interprer > Use the following Python interpreter and select there the path for the python.exe of the env you want to use.
For more info regarding how to use existing enviroments with Spyder: https://docs.spyder-ide.org/current/faq.html#using-existing-environment

pycharm and pyinstaller confusion

I have been writing .py scripts using PyCharm in Windows 10. I want to compile them into standalone .exe so I installed pyinstaller. I believe I made a mistake by installing it via the terminal inside PyCharm instead of cmd.
Pyinstaller was initially installed in C:\Users\myuser\PycharmProjects\routine\venv\Scripts
(\routine\ is the name of the project where I keep my scripts for tasks I do on a routine basis.)
When I DIR in \Scripts, I can see pyinstaller.exe
When I run pyinstaller from \Scripts, I get the following error:
Fatal error in launcher: Unable to create process using '"c:\users\myuser\pycharmprojects\routine tasks\venv\scripts\python.exe" "C:\Users\myuser\PycharmProjects\routine\venv\Scripts\pyinstaller.exe" ': The system cannot find the file specified.
If I try to run pyinstaller from a different directory, I get this message: "'pyinstaller' is not recognized as an internal or external command, operable program or batch file."
If I try to install pyinstaller from anywhere else, I get a bunch of messages saying "Requirement already satisfied: pyinstaller in c:\users\myuser\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages"
If I uninstall pyinstaller with pip and then reinstall it, I get this message "WARNING: The scripts pyi-archive_viewer.exe, pyi-bindepend.exe, pyi-grab_version.exe, pyi-makespec.exe, pyi-set_version.exe and pyinstaller.exe are installed in 'C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Scripts' which is not on PATH."
If I add the specified directory to PATH I still get the same message.
Any idea what I am doing wrong here, or any suggestions as to how to fix this?

Windows Task Scheduler to run Python Scripts

I am using VS Code to execute a python code. I want to automate this windows task scheduler,
however i am getting an error.
from VSCode this is what I see on terminal when I run the script;
Loading personal and system profiles took 712ms
(base) PS C:\Users\tableauautomation\Desktop\Python> & 'C:\Users\tableauautomation\Anaconda3\python.exe' 'c:\Users\tableauautomation\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\launcher' '64388' '--' 'c:\Users\tableauautomation\Desktop\Python\ForecastAutomation.py'
job Started: 2020-07-26 18:13:30
i am creating this in Notepad saved as bat but keep getting errors like Numpy or Panda's is not installed.
batchfile has this;
call activate my_env
"c:\Users\tableauautomation\Desktop\Python\ForecastAutomation.py"
pause
write the below lines in your notepad with .bat format.
call activate [my_env]
python c:\Users\tableauautomation\Desktop\Python\ForecastAutomation.py
call conda deactivate
if absolute path of python is not included in $PATH then use absoulte path of python.exe in .bat file.
call activate [my_env]
c:\Users\tableauautomation\Anaconda3\python.exe c:\Users\tableauautomation\Desktop\Python\ForecastAutomation.py
call conda deactivate
Anyone else have this issue the solution is here;
The batchfile needs the following parameter (Windows Machine)
cmd /c C:\Users\tableauautomation\Anaconda3\condabin\conda.bat run "C:\Users\tableauautomation\Anaconda3\python.exe" "c:\Users\tableauautomation\Desktop\Python\ForecastAutomation.py"
pause

Trying to run cygwin64 inside anaconda, can't get Tee command to run

I need to run a script (already given to me) called run_softmax.sh that runs some commands (including 'python') on my windows 10 machine. I have anaconda and cygwin64 installed. Inside anaconda, I first activate my python environment. Then I tried running C:\cygwin64\bin\bash run_softmax.sh but it gives me run_softmax.sh: line 13: tee: command not found
How can I add this functionality to cygwin?

os.popen doesn't work after compiler with pyinstaller

The following line is what I'm using to open the file in python, and this action called by a button(tkinter).
os.popen(r'"C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe" %s' %filename)
My development environment is Python3.7 + spyder 3.3.1 + windows 7 + latest version pyinstaller.
The issue is that this line can work well in spyder, but after compiler.
pyinstaller -F -w --onefile --icon=icon.ico ./DMS.py
I used this command to compile the Python script. When I click the button which I mentioned above, there is no any action be executed and no any error message pop up. I'm sure the application didn't hang on or crush.
When I try the following command to check what message on console.
pyinstaller -F DMS.py
The miracle happen, the PDF file can be opened without problem, and there is no any error message show on the console.
Does anyone know what things happen? and any suggestion for this case if show the console front of end user is not an option.
Many thanks.

Resources