Executing subprocess.Popen inside Python script in PyDev context is different than running in terminal - subprocess

I'm executing this code:
p = subprocess.Popen(['/path/to/my/script.sh','--flag'] , stdin=subprocess.PIPE)
p.communicate(input='Y')
p.wait()
It works when executing it on the shell using "python scriptName.py",
BUT when executing using PyDev in Eclipse, it fails, the reason:
/path/to/my/script.sh: line 111: service: command not found
This bash script "script.sh" contains the following command which causes the error:
service mysqld restart
So "service" is not recognized when running the .sh script from the context of PyDev.
I guess it has to do with some ENV VAR configurations, couldn't find how to do it.
BTW - Using "shell=True" when calling subprocess.Popen didn't solve it.

service usually is located in /usr/sbin, and that this directory isn't on the PATH. As this usually contains administrative binaries and scripts which arn't designed to be run by everyone (only by admins/root), the sbin directories arn't always added to the PATH by default.
To check this, try to print PATH in your script (or add an env command).
To fix it, you could either
set the PATH in your python script using os.setenv
pass an env dict containing the correct PATH to Popen
set the PATH in your shellscript
use the full path in your shellscript
set the PATH in eclipse

Related

Matlab doesn't recognize user environmental variable

I installed an application named lqns in the path: /home/robb/Research/dist/lqns-6.2/lqns (lqns is a folder containing the executable lqns). I want the program to be executed in command line simply calling lqns in the shell, I solved this adding to the file ~/.bashrc the line:
export PATH=$PATH:/home/robb/Research/dist/lqns-6.2/lqns
And it works with no issue. I am now trying to execute this program inside a Matlab script, running:
[status, ~] = system("lqns " + filename, '-echo');
Where filename is the path of an input file. I get the error message:
/bin/bash: line 1: lqns: command not found
Running the exact same command with the shell I get no error: the program runs with no problem generating the relative output.
Running getenv('PATH'); in Matlab and printenv PATH on my OS shell I indeed get two different results: Matlab does't have the path to lqns. I even tried editing manually the files /etc/environment, /etc/bash.bashrc and /root/.bashrc, with no result. How can I solve this issue?
you need to launch matlab by typing matlab in a terminal, not by double clicking on its shortcut from your desktop. (or even typing ./matlab in a terminal from your desktop)
it's up to the operating system to determine what double clicking does, and it's not guaranteed to execute most of your shell initialization scripts (or even launch it from the correct shell to begin with).
more info at Why are environment variables not resolved when double-clicking .desktop file?

VS Code - Using Code Runner Extension to Run Programs on Virtual Machine (VM)

I develop on Windows using a Linux VM via Vagrant and Virtual Box. I'm trying to figure out how to get the Code Runner extension to run my files on the VM. The biggest hurdle so far is, for a given file, I need to convert from the Windows host path to the Linux guest path.
Background:
The Code Runner extension allows one to map file types to shell commands to run those files. For example,
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt"
tells Code Runner that when I try to run a Java file, it should cd to the directory that contains the file, and compile the file, and then run the compiled file. The mapping from file types to commands is called the code-runner.executorMap which is contained in settings.json. By adding the option
"code-runner.runInTerminal": true
to my settings.json, I can tell Code Runner to run in the integrated terminal. So by simply SSHing into my VM from the integrated terminal via vagrant ssh, I have code runner targeting the virtual machine.
This is where the problem comes in - Code Runner is using my Windows style paths and my Windows file structures as command line arguments to my VM.
For example, suppose my Windows file structure looks like c:\a\b\c\d and my VM has its root in c so that c and d are shared folders. If I want to run a file in d, the command cd $dir will tell my VM to do cd c:\a\b\c\d.
I have thought of workarounds like adding the following to my settings to run python files
"python": "cd \"$(dirname \"$(locate -l1 $fileName)\")\"; python3 $fileName",
This command, which runs on the integrated terminal (the VM) locates and changes to the directory which contains the file which is to be run. It then tells the python3 interpreter to run that file. However, this doesn't always work (e.g. multiple files with the same name), and requires me to update the database that locate depends upon every time I add a file.
There has to be some way to translate my Windows file paths to the paths on the virtual machine (so e.g. c:\a\b\c\d -> /c/d). Perhaps through Vagrant? I would appreciate any help.
I developed a workaround. I would still be interested in a "cleaner" solution.
The workaround is as follows:
Firstly, I wrote a Python script to convert from Windows Paths to paths on my virtual machine. The script takes in the windows path to a file and the file name as arguments.
#pathconverter.py
import sys
windows_path=sys.argv[1]
file_name=sys.argv[2]
path_to_vagrantfile = r"C:\Users\Evan\Google Drive\Development\Vagrantfile"
slashes=path_to_vagrantfile.count("\\")
y=windows_path.split("\\")[slashes:]
linux_path="/vagrant/"+'/'.join(y) + "/" + file_name
print(linux_path)
Thus the following code converts from a windows file location to one on my virtual machine (assuming you saved pathconverter.py at the root of your shared directory, \vagrant :
python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName
Therefore, to run most files of various interpreted languages, I just supply the output of this command as an argument to the interpreter. For example, to run a Python script on my VM automatically, I just add the following line to the code-runner.executorMap:
"python": "python3 \"$(python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName)\""
Or for Racket/scheme, I just do:
"scheme": "racket \"$(python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName)\""

mongod and mongo commands not working on windows 10

I've installed mongoDB on my windows 10 OS. Then I tried setting its database path to some directory by moving to it and typing mongod --datapath=data in cmd, where data is the folder which is to contain the db(I've used the relative path because I'm in that directory). But message comes that mongod is unrecognized command. After some searching I found that by specifying mongod path, i.e. "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --datapath=data works. Similar thing happens for mongo.
I want to directly run mongod and mongo commands, I have seen people directly using it(without going to the directory or specifying the path).
For a Windows installation, by default you have to use the full path to the exe unless you add it to the PATH.
To add it to the PATH:
01) Get path to bin, something like: C:\Program Files\MongoDB\Server\4.0\bin
02) Press the Windows key, type env, select Edit the system environment variables
03) On the Advanced tab, click Environment Variables
04) In the User variables for xxxx section, select path and then click the Edit... button
05) Click New and paste your path with a trailing slash, eg:
C:\Program Files\MongoDB\Server\4.0\bin\
06) Click OK, OK, OK and restart your command window.
Source
The examples you have seen are probably based on UNIX installations which I think by default install mongo as a service (which Windows doesn't) and that is what is called in those examples.
To simplify startup and configuration on Windows, you can also install it as a service. See the Mongo documentation here and the
"Configure Windows Service for MongoDB' section".
This will then allow you to start and stop Mongo by simply calling
net start MongoDB
Or
net stop MongoDB
If installed MongoDB version is 6.0 or above, mongo command will not work on Powershell/cmd. If you run the command you will get the following error:
'mongo' is not recognized as an internal or external command,
operable program or batch file.
To run mongo commands, you have to install MongoDB Shell from
After installing the shell, extract the zip file, you can rename the extracted folder (mongosh-1.6.0-win32-x64) as "MongoDB Shell" and move that folder to Windows(:C) > Program Files
Now open the folder, go to bin and copy the path:
C:\Program Files\MongoDB Shell\mongosh-1.6.0-win32-x64\bin (or
C:\Program Files\mongosh-1.6.0-win32-x64\mongosh-1.6.0-win32-x64\bin)
Go to
Settings > System > About > Advanced system settings > Environment
Variables > Under System Variables, click on 'Path' then 'Edit' >
Click 'New' and paste the above copied path > Click 'Ok' 'Ok' 'Ok'
Now open Powershell/cmd, run the command 'mongosh'
You're all set to work with MongoDB
To add it to the PATH:
Add Mongo’s bin folder to the Path Environment Variable
Kindly check the link:
here
After adding bin folder to the path Environment Variable
then simply type mongo in terminal it will start working
reference : Microsoft document
set your path like this
;C:\Program Files\MongoDB\Server\4.0\bin
this is worked for me.
Based on welshGaz answer above, I edited the User Path variable but it did not work for me yet. I wasn't able to access the System Path variables.
What I noticed from the errors on the command prompt is that it what missing the "C:\data\db" directory to store its files (I don't know what those files are for just yet). So I created that directory myself and it worked.
Same problem here. I installed through the .msi file provided for windows X64bit. In the installer instructions from MongoDB (https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/), I read that you can add C:\Program Files\MongoDB\Server\4.2\bin to the System Path. Then it asks to omit the full path to the the MongoDB binaries. That is where I think some information is missing. How are we supposed to omit the full path to the MongoDB binaries?
Currently I can get MongoDB to run mongod using:
"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath="c:\data\db"
For --dbpath="c:\data\db" you can replace "c:\data\db" with the path to your database.
I can also run mongo using:
"C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe"
Another reason to it if you enabled any property in YAML file and it is not formatted properly. YAML looks for specific syntax like colon":"+space" ".
E.g.-
security:
authorization: enabled
use mongosh command from your terminal. mongo command no longer works for 6.0 and above.
if you are trying to connect from connection url eg mongodb://localhost:27017/yourdb try changing it to something like mongodb://127.0.0.1/yourdb

Ember.js fails to install globally

I ran the command to install Ember.js:
npm install -g ember-cli
Then when I run:
Ember -v
I get error: "The term ember is not recognized as the name of a cmdlet, function, script file or operable program ..."
I added the system environment variable $NODE_PATH = %AppData%\npm\node_modules
I see ember-cli folder in the $NODE_PATH
This is a newly imaged machine so this may be an issue with my npm setup/configuration. How can I install ember globally?
I added %AppData%\npm (use the full path which in my case is C:\Users\bmackey\AppData\Roaming\npm) to the system Path environment variable. I had to remove C:\Program Files\Microsoft DNX\Dnvm\ from my Path in order to stay under the 260 character limit; hopefully I won't need this. I do not know a way around the 260 character limit.
Now ember -v works.
You need to add the path to the ember.cmd file into the powershell environment variable, note that this is different to the standard PATH environment variable.
For those that don't know, the reason you can run ember (a node module) simply by running the command ember is because when you install ember it create a ember.cmd file in your AppData folder:
this file typically looks like this and just bootstraps node and runs the ember js file:
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\node_modules\ember-cli\bin\ember" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\node_modules\ember-cli\bin\ember" %*
)
so when you run ember from your command window or powershell it just looks for a cmd file in its PATH variable. If this doesn't have an entry pointing at the location of this cmd file it won't be able to run it.
To fix this in powershell just run the following:
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Users\<user>\AppData\npm", [EnvironmentVariableTarget]::Machine)
Most of this is taken from the answer here.
Make sure C:\Users\<user>\AppData\npm is where NPM has deployed your ember.cmd file. I have seen this also deploy in C:\Users\<user>\AppData\Roaming\npm so it can vary.

lost PATH while trying to set custom winlogon shell in WindowsXP

I have changed the shell key in windows registry to gain custom shell (Kiosk usage):
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Winlogon]
I set shell key to a batch file which runs two applications as below:
start "myFirstAppTitle" "myAppPath\myApp1.exe"
start "mySecondAppTitle" "myAppPath\myApp2.exe"
Each application runs but the second application which needs some files to be excuted throws an error which says could not find dependency files. whereas the dependency files are adjoining to the exe file and the mentioned app works fine, when starts from startup.
Meanwhile when i run the batch file manually it rusn fine.
I added the PATH command to the batch file but it did't work too.
Change the batch file to this:
set PATH=%PATH%;C:\MyAppPath
start "myFirstAppTitle" "myApp1.exe"
start "mySecondAppTitle" "myApp2.exe"
If you start executables without an absolute path, the path is relative to the current working directory. Also, when you specify an executable with a relative path, %PATH% is not searched for a matching subfolder with a matching executable.
Since the script worked when you manually started it, your working directory probably was C:\. However, when run at logon as a replacement shell, the working directory is most likely "%SystemRoot%\system32".
The problem solved strangely, i removed the title parameter of start command and it worked. In fact i used start command this fashion:
set PATH=%PATH%;C:\MyAppPath
start myapp.exe
start myapp2.exe