impala shell, shell command with capitals - python

I've got a lot of commands running in impala shell, in the middle of them I now have a need to run a python script. The script itself is fine when run from outside the impala shell.
When I run from within the impala shell using ! or "shell" (documentation found here) it changes the commands to be fully lower case.
The path to the script itself would be something like this: /home/DOMAIN_USERS/somemorefolders/python/script.py
so in my impala shell I'm running: !/home/DOMAIN_USERS/somemorefolders/python/script.py
the error I get back is
sh: /home/domain_users/somemorefolders/python/script.py: No such file
or directory
Is there any way to force it to not make it into lower case? I've tried putting both single & double quotes round the path but that makes no difference.
I guess if there's no way I'll have to come out of the impala shell, run the python bit then go back in. Its just a bit more work when I figured the "shell" command in the impala shell is there for that exact benefit.

This is caused by a known bug IMPALA-4664.
A workaround is to leave a space after "!". Can you try this (note the space):
! /home/DOMAIN_USERS/somemorefolders/python/script.py

Thanks to #BoboDarph for help in getting there.
I was able to use !~/somemorefolders/python/script.py as I could get there from my home directory.
I still think it's a bit shortsighted of impala to force things into lower case but there you go.

Related

How to use VS Code - Python Shell

So I am relatively new to programming and used to use Python's own "IDLE". After I run a ".py" file with IDLE, I am used to getting a python shell or a command window, I don't really know the terminological name for it, where I could play around with the objects inside the script.
For example, if I had a list A=[1,2,3] inside the program, after I run it I get a command console that says ">>" and I can say ">>A" which gives me "[1,2,3]" and I can add elements to A etc.
Now, I want to start using VS Code but I can't seem to find that particular thing. I have a terminal where I can run python code if I give the command "python" first, but It doesn't seem to effect anything inside the script.
I want to use that to see if some objects are working fine and to check their types etc. I add more lines to code after I try from there first, if that makes sense.
Sorry for really bad terminology, I really don't know the names but I can try even more if it's not clear.
Thanks a lot in advance.
Are you looking for the Integrated Terminal of VS Code?
Here are some ways to open the terminal:
Use the ⌃` keyboard shortcut with the backtick character.
Use the View > Terminal menu command.
From the Command Palette (⇧⌘P), use the View: Toggle Integrated Terminal command.
In the window that shows up, enter python and you'll get the Python shell you're looking for.
Try using the integrated terminal inside vs code and make sure that python and pip are properly configured. Type python in the command line and make sure the terminal points to the same folder where your program file is located.

Python in VSCode: How do you run a previous command?

I'm kind of new to software development. Outside of VSCode, I can open up a terminal (let's say PowerShell), run python in it, type in a command (like 2+2), be able to click the up arrow key to find my previous command so that I can run it again.
If I run PowerShell in VSCode and do the same thing, nothing happens when I click the up arrow where I would expect my previous command to be cycled.
Is this a problem with my Python or VSCode? I've been looking for a solution for this but haven't found many useful topics on this.
This was going to just be a comment but its too long. Sorry it isn't more informative than it is. This is kind of an odd problem because VSCode isn't a true IDE. It doesn't have its own shell and just hijacks your powershell or bash terminal, depending on which OS you are using. You should be able to use your up and down arrows just like you can in powershell. I have tested it on my own VSCode installation and it works fine for me. If it's a problem, it's not with python, since VSCode will interact with the terminal the same way no matter which language you are using it for, so its probably with VSCode or your terminal. I have heard of others having issues with up arrow autocomplete in bash, so if you are connecting to a bash terminal that could be it, but I've never heard of it glitching in powershell. I'd say check which terminal you are using, see if the problem persists when you change terminals, and try reinstalling VSCode if it does. Past that, I don't know what to tell you.
I found a work-around. For me, neither git bash, nor PowerShell allowed up/down arrows for history switching within a python shell. So here it goes.
Ctr-Shift-P opens VSCode commands
Python: Create Terminal does not actuallt start python, but it does launch powershell in a mode that will enable us to succeed
py starts python shell with working up/down arrows!

Unable to source .profile in shell script

To start off, I am a complete noob to Debian, Python, and shell scripts, so please speak to me like I am a toddler.
I have a python script I am running through a virtualenv, and I want to execute it via a shell script. Here is what I'm typing in to the terminal to do so:
source .profile
workon cv
cd Desktop/Camera
python main.py
I tried turning this into a shell script, but I receive the error -- source: not found
I've found no answer to my problem, at least not in any terms I can understand. Any advice would be appreciated. Furthermore, before you answer, I also have no idea why it is I need to execute source .profile, I'm simply following a beginner guide for the project which can be found here: https://www.hackster.io/hackerhouse/smart-security-camera-90d7bd
Thanks in advance, and sorry if this is a dumb question.
Best practice for Shell would be shebang at the begging like #Veda suggested.
Execute the shell script using bash like bash shell.sh as the link suggests using relative locations rather than absolute ones
Add a hashbang at the top of your script (should be the first line):
#!/bin/bash
This will ensure you are running your shell-script in bash. Having a shell does not mean it's bash. Not all shells have the source function that you are using (bash has it).
Some prefer the following:
#!/usr/bin/env bash
Since you are a "beginner" I think it does not really matter. The first makes sure it's using the bash in /bin, the second is using the PATH variable to find bash, so the user of your script can provide it's own bash if he/she wants.

Running Rscript via Python using os.system() or subprocess()

I am facing problems running a Rscript via Python using os.system() or subprocess().
Using os.system() to run commands via python works generally fine for me (e.g. with gdalwarp.exe) but not with Rscript.exe.
The only difference I can see are spaces in the path.
Avoiding problems with spaces in the path are easy overcome in the CMD-window by putting the paths in quotation marks.
Executing the following command is successfull.
"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"
But I am stuck with Python.
What I tried so far with python:
os.system("C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r'"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"')
subprocess.call([r'C:/Program Files/R/R-3.0.2/bin/Rscript.exe', r'D:/.../otsu_Script.R'])
Does anybody see what I am doing wrong?
Thanks in advance,
Eike
After getting mental on such a simple problem. I decided to reinstall RStatistics to a path with no spaces or points, like:
C:/R/bin/Rscript.exe.
Now
subprocess.call(["C:/R/bin/Rscript.exe", "D:/otsu_Script.R"] ) or
os.system("C:/R/bin/Rscript.exe D:/otsu_Script.R") are working just fine.
Should have tried it two days ago...
... but now I am a happy monkey anyway :-)
It probably is way too late now and I have seen you solved the issue, but I was having a similar issue (although in a Linux system) and it might help someone else now; this command was not working when called inside python although it worked directly on the terminal/command-line.
os.system("R CMD BATCH ./read_lengths_batch.R")
I tried many solutions, including subprocess and others but found it to be easier than that. In my case, and I understand it might be different in Windows, I just had to add a & at the end of the call for it to run in the background. Somehow it seemed R would shut down with the Python script instead of doing its work.
os.system("R CMD BATCH ./read_lengths_batch.R &")
Strangely, it was also working if in my folder I would have the same file copied with a .txt extension: read_lengths_batch.R and read_lengths_batch.txt.
Hope it helps someone!

Some powershell cmd-lets not available if run from python?

So this is an unusual one, and perhaps I am simply missing the obvious, but I have the following python code that creates a powershell script and runs it.
# Create the PowerShell file
f = open("getKey.ps1", "w")
f.write('$c = Get-BitlockerVolume -MountPoint C:\n')
f.write('$c.KeyProtector[1].RecoveryPassword | Out-File C:\\Temp\\recovery.key\n')
# Invoke Script
startPS = subprocess.Popen([r'C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe',
'-ExecutionPolicy', 'Unrestricted', './getKey.ps1'], cwd=os.getcwd())
result = startPS.wait()
When this is run, it gives me the following error:
The term 'Get-BitlockerVolume' is not recognized as the name of a cmdlet, function, script file, or operable program.
However, if I then go and manually run the generated script, it works perfectly. To add to the oddity, if I run the same command exactly as above ie:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted ./getKey.ps1
it also works exactly as expected.
Clearly, the above error is a powershell error, so it is successfully running the script. It almost seems like powershell somehow knows that this is being run from python and has some restricted library of commands when a script is run from a particular source. I grant that that idea makes no real sense, but it's certainly how things appear.
I don't think this is a permissions issue, because when you run the same command from an unelevated powershell prompt, you get an Access is denied type error, rather than a command doesn't exist kind of error.
Anyway, any help would be greatly appreciated!
Edits
Edit: New evidence to help figure this out:
It's definitely an issue of cmdlets being loaded properly. If I programmatically run a script to dump the list of all available commands to a text file, it is only about 2/3's as big as if I do so through a powershell prompt directly
I bet Python is running as a 32-bit process on 64-bit Windows. In this case, you'll end up running 32-bit PowerShell, which in practice is a Bad Thing since many PowerShell modules depend on native binaries that may not have 32-bit equivalents. I hit this with IIS Manager commandlets--the commandlets themselves are registered in 32-bit PowerShell, but the underlying COM objects they rely on are not.
If you need to run 64-bit PowerShell from a 32-bit process, specify the path as %SystemRoot%\SysNative\WindowsPowerShell\v1.0\PowerShell.exe instead of System32.
System32 is actually virtualized for 32-bit processes and refers to the 32-bit binaries in %SystemRoot%\SysWow64. This is why your paths (and PSMODULEPATH) will look the same, but aren't. (SysNative is also a virtualized path that only exists in virtualized 32-bit processes.)
Adding to what #jbsmith said in the comment, also check to make sure that the environment variable that PowerShell relies on to know where it's modules are is populated correctly when python starts the process.
%PSMODULEPATH% is the environment variable in question, and it works the same way the %PATH% variable does, multiple directories separated by ;. Based on what you say your observed behavior is, it seems that you are using PowerShell 3.0, and cmdlet autoloading is in effect.
The solution here: Run a powershell script from python that uses Web-Administration module got me the cmdlet I needed, however there are still missing cmdlets even when using this method. I'm still at a loss as to why some are loaded and others are not, but for the time being, my script does what I need it to and I can't spend any more time to figure it out.
For reference here is the code that worked for me
startPS = subprocess.Popen([r'C:\Windows\sysnative\cmd.exe', '/c', 'powershell',
'-ExecutionPolicy', 'Unrestricted', './getKey.ps1'], cwd=os.getcwd())
I had the same issue, and it was simply that the BitLocker feature was not installed, hence the module wasn't present.
I fixed it by installing the Bitlocker feature:
Windows Server:
Install-WindowsFeature BitLocker -IncludeAllSubFeature -IncludeManagementTools -Restart
Windows Desktop:
Enable-WindowsOptionalFeature -Online -FeatureName BitLocker -All

Categories

Resources