I used to run a program called vina through the cmd line using the following command:
"\Program Files (x86)\The Scripps Research Institute\Vina\vina.exe" --config conf.txt --log log.txt
So, the program takes the config file to run and outputs a log file with the results. I am running the program in python using the os.system module. However, I can't assign the config file or the log file as I did in the cmd. I tried something like this in python:
os.system('C:/Program Files (x86)/The Scripps Research Institute/Vina/vina.exe' --config conf.txt --log log.txt)
The program opens real quick then closes, maybe because it doesn't run the config file. I have also tried putting the config into a string as follows (I am omiting the log thing until I get the config to work):
os.system('C:/Program Files (x86)/The Scripps Research Institute/Vina/vina.exe', '--config conf.txt')
In this case I get the error:
TypeError: system() takes at most 1 argument (2 given)
Any ideas on how to specify the config file and the log file output in python lines as I do in cmd?
The correct syntax to replicate the command line command would be
os.system('"C:/Program Files (x86)/The Scripps Research Institute/Vina/vina.exe" --config conf.txt --log log.txt')
If that doesn't work, try specifying full paths for conf.txt and log.txt
os.system(r'"C:/Program Files (x86)/The Scripps Research Institute/Vina/vina.exe" --config conf.txt --log log.txt')
Related
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 ...
I have a self-installed python in my user directory in a corporate UNIX SUSE computer (no sudo privilege):
which python
<user>/bin/python/Python-3.6.1/python
I have an executable (chmod 777) sample.py file with this line at the top of the file:
#!<user>/bin/python/Python-3.6.1/python
I can execute the file like this:
python sample.py
But when I run it by itself I get an error:
/full/path/sample.py
/full/path/sample.py: Command not found
I have no idea why it's not working. I'm discombobulated as what might be going wrong since the file is executable, the python path is correct, and the file executes if I put a python command in the front. What am I missing?
EDIT:
I tried putting this on top of the file:
#!/usr/bin/env python
Now, I get this error:
: No such file or directory
I tried this to make sure my env is correct
which env
/usr/bin/env
EDIT2:
Yes, I can run the script fine using the shebang command like this:
<user>/bin/python/Python-3.6.1/python /full/path/sample.py
Your file has DOS line endings (CR+LF). It works if you run python sample.py but doesn't work if you run ./sample.py. Recode the file so it has Unix line endings (pure LF at the end of every line).
Try using #!/usr/bin/env python as described in this post. Let the OS do the work.
I have a script.py in /Users/admin/Desktop and I want to run this script on a file that is in /Users/admin//Desktop/folder/file.txt, without changing the present dir.
Question: what is the most efficient way to do that on command-line ? I am using the following commands and results are not as expected.
$ python script.py --script; /Users/admin/Desktop/file.txt
raise StopIteration('because of missing file (file.txt)')
StopIteration: because of missing file (file.txt)
Remove the semicolon because that will prematurely terminate the command.
Pass the correct path to the file to your program. You say it is /Users/admin/Desktop/folder/file.txt, however, your command is using /Users/admin/Desktop/file.txt (it's missing folder)
So the command should (probably) be:
$ python script.py --script /Users/admin/Desktop/folder/file.txt
If that doesn't work you will need to edit your question to show your code.
I am trying to use Python Fabric to copy a file from Windows to a debian system.
SOURCE: The Windows folder is C:\Users\UserN\Downloads contains the file test_celsius.out.
DESTINATION: The Debian folder is /mnt/Reado/RoTempValC.
I can move other files from the SOURCE to the DESTINATION using WinSCP. However, I need to use Fabric to move this particular file.
I can use Fabric to change into this directory and list its current contents:
ls /mnt/Reado/RoTempValC
Here is what I have tried - in a Fabric task named move() I have this
run('mv C:\Users\UserN\Downloads\test_celsius.out /mnt/Reado/RoTempValC')
Now, here is the output:
.
.
.
.
[10.10..] Executing task 'move'
[10.10..] run: mv C:\Users\UserN\Downloads\test_celsius.out /mnt/Reado/RoTempValC
[10.10..] out: mv: rename C:/Users/UserN/Downloads/test_celsius.out to /mnt/Reado/RoTempValC/test_celsius.out: No such file or directory
[10.10..] out:
Disconnecting from 10.10.. done.
Fatal error: run() received nonzero return code 1 while executing!
Requested: mv C:/Users/UserN/Downloads/test_celsius.out /mnt/Reado/RoTempValC
Executed: /bin/bash -l -c "mv C:/Users/UserN/Downloads/test_celsius.out /mnt/Reado/RoTempValC"
Aborting.
I am not sure why it is doing this. I can correctly list the contents of the directory in Debian by using the ls command above.
Is there a way to copy this file?
EDIT:
Additional Information:
I am running the above fab move command from the Windows command
prompt.
I opened the command prompt and typed cd Python27\SGTemp
since this is where the fabfile.py is located.
Then I ran fab move.
EDIT 2:
I replaced /mnt/Reado/RoTempVal by /mnt/Reado/RoTempValC/ but got the same output as above.
Try fabric.operations.put(*args, **kwargs):
put('C:\Users\UserN\Downloads\test_celsius.out', '/mnt/Reado/RoTempValC')
I'm trying to run wlst script form .py file but it can not be done
Content of .py file :
connect('weblogic','weblogic','t3://localhost:8001')
sca_undeployComposite('http://localhost:8001','Hello','1.0','user='weblogic',partition='myPartition')
sca_deletePartition('myPartition')
sca_createPartition('myPartition')
sca_deployComposite('http://localhost:8001','C:\WLST\Test\Application.zip',user='weblogic',configplan='myPlan.xml', partition='myPartition')
exit()
when i run cmd file to execute script, Only connect() method is execute success. any command bellow it can not be execute. And error message appear: Problem invoking WLST - Traceback (innermost last): File "c:\WLS\script\filname.py", line 2, in ?
Name Error: sca_undeployComposite
Please help me to resolve it. Thanks !
The commands after the connect() line which are not regular WLST commands. They requires sca related libraries into CLASSPATH. if you look into your wlst.cmd or .sh file that is actually calling the environment setup file that could be setWLSEnv.sh/.cmd. If you run that from where you are having the this python script. That script will work, it is simple java CLASSPATH funda nothing else!
Probably you might be running wlst.cmd after navigating to the common bin folder like
cd /oracle/fmwhome/Oracle_SOA1/common/bin/.
instead you can run in your script like this
C:\WLS\script\>/oracle/fmwhome/Oracle_SOA1/common/bin/wlst.cmd filename.py
or
C:\WLS\script\>/oracle/fmwhome/Oracle_SOA1/common/bin/setWLSEnv.cmd
C:\WLS\script\>java weblogic.WLST filename.py
You can also refer for more sca related scripting: WLSTByExamples