Python virtualenv / virtualenvwrapper workon xxx - python

Working a project with a development track that requires a 'workon xxx' to build the development track and a 'deactivate' to go back to the standard python to build the stable track. Using the Windows command line switching between python environment works great. However, when using 'workon xxx' from a batch script the it it doesn't return to execute the next line. It drops to the command line. I have tried 'call workon xxx' and the result is the same.
How can 'workon xxx' be called from a batch script and return to execute the next line?

call should work just fine:
c:\srv\tmp> cat workoncall.bat
#echo off
call workon dev
call cdsitepackages
echo %CD%
call workon pydeps
call cdsitepackages
echo %CD%
(pydeps) c:\srv\tmp> workoncall.bat
c:\srv\venv\dev\Lib\site-packages
C:\srv\venv\pydeps\Lib\site-packages

Related

Windows .bat file to run python script

Try to create a Windows .bat file to achieve the below function:
cd C:\repo\demo
venv\Scripts\activate
python test.py
In Visual Studio Code terminal window, I can run the above lines without issue.
Created a .bat file as below:
cd C:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "python test.py"
pause
When double click the above .bat file to run it, end with error:
if [ "${BASH_SOURCE-}" = "$0" ]; then
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Also tried the below .bat code, not working either:
cd C:\repo\demo
venv\Scripts\activate
python test.py
pause
How to correct the .bat file to make it work?
======================================
Based on #Compo's comment, tried the below version and it successfully executed python test.py:
cd C:\repo\demo
call "venv\Scripts\activate.bat"
python test.py
pause
but seems it didn't finish call "venv\Scripts\activate.bat", the command line window shows as below:
When manually run the code, it will prefix the path with (venv) as below which shows the proper result:
============================================
UPDATE:
The below .bat version works now, an answer from this question
cd C:\repo\demo && call "venv\Scripts\activate.bat" && python test.py && pause
You should either remove "C:\...\python.exe" from the second line:
cd C:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
python heatmap.py <-- like this
pause
or remove python
cd C:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "heatmap.py"
pause
Try this:
cd c:\python
python scripts/test.py <-- here, you can replace "test" with the name of the Python program(script) you want to execute you

Create terminal name for execute python script in Ubuntu

I have a python file in: '/home/username/scripts/pyscript' and I want set a word for execute directly this script.
I want do this "python3 /home/username/scripts/pyscript/main.py arg1 arg2" but looks like
this "myscript arg1 arg2"
Is this posible?
Thank you anyway.
It is possibile in a number of ways. Links are for Bash, supposedly your shell but the ideas always apply.
First option: make a shell alias
alias myscript='python3 /home/username/scripts/pyscript/main.py'
Be sure to add the alias to your .profile to make it survive logout.
Second option: define a wrapper script. Create a file with the following content, named after your desired command (e.g. myscript):
#!/bin/bash
python3 /home/username/scripts/pyscript/main.py "$#"
save it and make it executable, then call it :
chmod +x myscript
./myscript arg1 arg2
Be sure to copy the script in a folder in your PATH (check where with echo $PATH) to be able to call it from any folder.
You can also use pyinstaller to create a single file executable:
Step 1: Install pyinstaller
[Note: best practice is to do this in a virutalenv]
$ pip install pyinstaller
Step 2: Run pyinstaller against your script
$ pyinstaller --console --onefile /home/username/scripts/pyscript
$ pyinstaller pyscript.spec # use this after the first run
Step 3: Test the generated executable
$ cd /home/username/scripts/dist # generated by pyinstaller
$ pyscript arg1 arg2
Step 4: Leverage the $PATH variable
$ cp /home/username/scripts/dist/pyscript /usr/bin
You should now be able to run the executable from anywhere.
It should be noted that the executable that is generated is OS specific. For example, if you generate it on an Ubuntu machine, it will only run on Ubuntu (Debian based). The same holds true for Windows and other Linux distros.
Finally I solver with the help of #pierpaciugo
I add a alias at the end of the .bashrc for make it persistent:
alias create='bash /home/username/Programming/Python/GithubAPI/script.sh'
I couldn't use only alias because I have my python dependencies on a virtual environment so if I try this i could not add params to my python script.
For that I create this bash script:
#!/bin/bash
source /home/username/Programming/Python/GithubAPI/venv/bin/activate && python3 /home/username/Programming/Python/GithubAPI/main.py $# && deactivate
Now I can write "create param1 param2" and it works.
I am using all global paths but could be a good idea add the script in a folder in my PATH.

Batch file logic failing

I'm trying to get a batch-file to execute the following code:
cmd /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
cd C:\ProgramData\Anaconda3\Lib\site-packages\tabpy_server\
run startup.bat
The first line execute properly and opens an anaconda python command-prompt window. The next lines fail to execute.
What am I missing?
The idea is to create a batch file which can be added to windows task-scheduler to start the tabpy server service.
There is no run command in batch. What you want is the start command. Also you want to use call instead of cmd /k because it is used for starting another instance of cmd while you just want to call the batch file. Here is an example:
call C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
cd C:\ProgramData\Anaconda3\Lib\site-packages\tabpy_server\
start startup.bat

Run python module from command line

I don't really know how to ask this question but I can describe what I want to achieve. I would update any edits that would be suggested.
I have a python module that makes use of some command line arguments. Using the module requires some initial setup outside of the python interpreter. The python file that does the setup runs fine, but the problem is that I have to dig through the python installation to find where that file is located i.e. I have to do python full-path-to-setup-script.py -a argA -b argB etc.I would like to call the setup script like this
some-setup-command -a argA -b argB etc.
I want to achieve something like
workon environmnent_name as in the virtualenv module or
pipenv install as in the pipenv module.
I know both of the above commands call a script of some kind (whether bash or python). I've tried digging through the source codes of virtualenv and pipenv without any success.
I would really appreciate if someone could point me to any necessary resource for coding such programs.
If full-path-to-setup-script.py is executable and has a proper shebang line
#! /usr/bin/env python
then you can
ln -s full-path-to-setup-script.py ~/bin/some-command
considering ~/bin exists and is in your PATH,
and you'll be able to invoke
some-command -a argA -b argB
It's a bit difficult to understand what you're looking for, but python -m is my best guess.
For example, to make a new Jupyter kernel, we call
python -m ipykernel arg --option --option
Where arg is the CLI argument and option is a CLI option, and ipykernel is the module receiving the args and options.
Commands that are callable from the command prompt are located in one of the directories in your system's PATH variable. If you are on Windows, you see the locations via:
echo %PATH%
Or if you want a nicer readout:
powershell -c "$env:path -split(';')"
One solution is to create a folder, add it to your system's PATH, and then create a callable file that you can run. In this example we will create a folder in your user profile, add it to the path, then create a callable file in that folder.
mkdir %USERPROFILE%\path
set PATH=%PATH%%USERPROFILE%\path;
setx PATH %PATH%
In the folder %USERPROFILE%\path, we create a batch file with following content:
# file name:
# some-command.bat
#
python C:\full\path\to\setup-script.py %*
Now you should be able to call
some-command -a argA -b argB
And the batch file will call python with python script and pass the arguments you added.
Looking at the above answers, I see no one has mentioned this:
You can of course compile the python file and give executable permissions with
chmod +x filename.py
and then run it as
./filename.py -a argA -b argB ...
Moreover, you can also remove the extention .py (since it is an executable now) and then run it only as
./filename -a argA -b argB ...

workon command not found when using fabric

My fabric file:
def deploy():
code_path = 'mysite/public_html/mysite'
with cd(code_path):
with prefix("workon mysite"):
run('git pull')
run('supervisorctl -c ~/supervisord.conf restart ' + env.host_string)
I get the following error:
Aborting.
[myserv] out: /bin/bash: workon: command not found
Obviously workon command works when I do this manually (without fabric). I suspect /usr/local/bin/virtualenvwrapper.sh is not being sourced (it normally gets run through .bash_profile).
What do I need to do to get workon command working?
Try modifying your prefix with:
with prefix(". /usr/local/bin/virtualenvwrapper.sh; workon mysite"):
you have to copy this virtualwrapper load code from .bashrc to .bash_profile file or if not exist create new .bash_profile file and copy there.
code to copy::
export WORKON_HOME=/home/virtual
source /usr/local/bin/virtualenvwrapper.sh
this error happen because .bashrc is only read by a shell that's both interactive and non-login. So in this case it is not interactive non-login shell, so it won't work. so we have to copy those code to .bash_profile file.
reference link
I use pyenv with plugin pyenv-virtualenvwrapper. I had no success with workon, instead I use this (fabric 2.5):
with c.prefix('source /home/mirek/.virtualenvs/%s/bin/activate' % PROJECT):
with c.prefix('cd /home/mirek/dj/%s/%s' % (PROJECT, PROJECT)):
c.run('python manage.py ....')

Categories