How to run git-bash commands from Python Fabric on Windows - python

We're using Python 2.7 Fabric 1.13 on Windows 12 Server, on which is also installed Git-Bash for it's almost linux-like terminal. Amazingly, we've gotten it all to work - almost. It's a hodgepodge, but seems less clunky than Cygwin.
I have a script in my user's ~/.bash_profile which works fine when I run it manually inside of Git-bash CLI. This script is a local preparation for what is to come next.
When I try to use Fabric's local command to run this function, it just errors with return code 1. I'm assuming that my function, a function in my user's environment, is not present when Fabric is running.
How do I find the user that my fabric script runs as (again, on Windows)? And more to the point, how do I inject this code (function) into that user's environment? Or, how do I make it global to all users?

Related

Run Python script that affect parent shell (changes environment variables, runs other scripts, etc.)

Recently I need to affect bash shell that runs python script from python script itseft. I develop a Python utility package that add some additional functionallity to pip. One of the workflows in this package needs to active Virtualenv to work as planned. Here is the problem.
When you run something like:
os.system('/bin/bash ./venv/bin/activate')
Or:
subprocess.Popen(['/bin/bash', './venv/bin/activate')
It doesn't do anything to the shell when the script is executed. Basically because these commands are executed in isolated processes (I guess) and therefore does not affect bash process itself.
Question: how can you affect parent shell that execute python script from inside the script (add some environments, run other script, etc.)?
Thanks in advance.
how can you affect parent shell that execute python script from inside the script (add some environments, run other script, etc.)?
It is not possible to do that, unless your operating system is broken. Process isolation is one of the very basic concepts of an operating system.
Instead, resaerch what venv does and how it works and what activate script does and just add the proper directory to python module search path.

Run Python script as a Windows service on a non-Python PC

I am looking to run my new Python script as a service. The problem is the users most definitely would not have Python installed. After doing research I found the two main options: nssm and the win32serviceutil code, but as far as I understand both require a Python installation on the PC, since you have to specify the path to your python.exe.
Is there any other way to make a Python script to run as soon as Windows is started and run in the background, which doesn't require an existing Python installation?

PyCharm calls packaging_tool.py list again and again

I have several user accounts on my linux PC.
Each is running a virtual env in $HOME.
PyCharm works nice in several of them.
But in one virtualenv it constantly uses a lot of CPU. PyCharm does react very slow, and sometimes not at all concerning searches ("find Usage" or "ctrl-shift-f").
Normal editing works fine.
But double-click on a class name to jump to the definition of the class does not work.
I checked what's wrong with the command line tool top.
I see that this gets executed over and over again:
python /snap/pycharm-community/117/helpers/packaging_tool.py list
The process has a new PID every second.
The virtualenv is on a local SSD disk.
What could be wrong here?
Versions: PyCharm 2018.3.5, Python 2.7, Ubuntu 18.04
I removed ~/.PyCharmCE2018.3/ and started again from scratch. Same issue again.
Here is the output of above script: https://pastebin.com/StPqJns6

python flask how to access CLI from terminal when running application from Pycharm

I am using python 2.7 with flask and using pycharm professional IDE, I am running the flask application using a virtual environment from inside pycharm.
When I open a terminal inside pycharm and use CLI commands, it works, and when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working, the app is still running all the time, and the command is excactly the same.
When I try to activate the same venv outside of the IDE i get permission issue, I assume that it has to do with the venv already being active inside the IDE.
What is the issue? I need to run the same virtual environment in order to use the CLI commands?
How can i access the CLI commands from outside of the IDE?
Thanks
When I try to activate the same venv outside of the IDE I get permission issue
Most likely the problem is all about permission access to the virtual environment's files. Check out access permissions and user:group ownership using ls -al (if you're on Mac or Linux), more info here:
https://linux.die.net/man/1/ls
https://linux.die.net/man/1/chmod
https://linux.die.net/man/1/chown
I assume that it has to do with the venv already being active inside the IDE
Definitely not, you can activate it as many times as you want.
I need to run the same virtual environment in order to use the CLI commands?
At least you have to have all the dependencies installed in your other environment (global or virtual) if you have plans to use one.
when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working
You'd better post a full error output so that we could check the actual error. Also what command are you trying to run?

Setting default system-wide environment for Python

My company releases part of its product as Python scripts running on an embedded Linux-based system. For our production release, I want to set the environment variable PYTHONOPTIMIZE="1" so that all Python scripts will be run as if the "-OO" switch were specified.
However, the scripts will be running by a user who isn't logged in (for example, some are invoked via the web server and others are started via systemd) so I can't simply set PYTHONOPTIMIZE in, e.g., /etc/profiles.
One solution might be to modify the shebang in each Python script to include the "-OO" switch but this seems a little involved. Is there an obvious way to make Python execute all Python scripts using a default environment that I missed?

Categories

Resources