I am trying to run a bat script via Powershell that uses Python calls, using Cygwin's Python. The script is pre-made (i.e. not mine) and rather large, but since it invokes python, rather than python.exe calls, it does not execute properly, since the proper command would be python27.exe. How can I change that in Powershell?
I guess you can create an alias for that in your PowerShell script or profile.
Something like below could be an option for you (include this at the top of your PowerShell script, before the first call to python):
New-Alias -Name python -Value "C:\Program Files\Python\Python27.exe"
Also check in help in PowerShell itself for this.
Get-Help about_alias
https://technet.microsoft.com/en-us/library/hh849959.aspx
Related
Background:
I'm using Windows. I know some of programming with python. I don't know much about batch, which I think I might need to do what I want.
I will show a example so it becomes more clear what I'm trying to do.
Example:
When using git, after you install it, you can call the git command from anywhere of your computer, you can execute git commands, like git init and this will create a git file in your current folder.
I don't know exactly how git works or what language they use but I want to do the same thing, create my own command that I can execute from anywhere in my computer after I "install" my program.
What I'm trying to do:
I want to make my own command and when I call it, it executes a python script.
e.g.
I install my program and it creates a command called myprogram and when I type myprogram in the command line, it's like if I typed python myprogram.py. And myprogram -someargument would be the same as python myprogram.py -someargument.
What I tried until now:
I'm searched for How to make a environment variable that runs Python script? but I never get exactly what I want, is always something like How do I set environment variable using Python script?.
Maybe I'm making the wrong question and the result I want are not showing?
I'm looking for a explanation on how to do this or at least a tutorial/guide.
Edit 1:
As UnholySheep said in the comments, it's not environment variable, its commands, so I changed the question even does what I want to know is the same thing.
Files you need:
First you need a python script (obviously) so I created a file called myprogram.py that have this simple line:
print("This should be from a command")
After you need to make a batch file, in my case I used a .cmd file called myprogram.cmd that have:
#ECHO OFF
python_directory\python.exe python_script_directory\myprogram.py %*
Configurations to make:
You need to set in PATH environment variable the location of the batch file batch_file_directory\myprogram.cmd
And now if you execute in the command line myprogram it will print This should be from a command.
You can also use .exe or .bat files.
Question
My default Python is 2.7, but I have a script that requires Python 3.4. I am trying to create a function in R that will:
Switch to Python 3.4
Run this script
Switch back to Python 2.7
Import results into R
To switch between Python versions, I use my cluster's "dotkit" system, like this:
use Python-2.7
use Python-3.4
"use" is a bash function that is imported in my .bashrc file. It sets all of my path variables (PATH, LIBRARY_PATH, LD_LIBRARY_PATH, CPATH, C_INCLUDE_PATH, etc). The problem is that when I try to call this function in R, I get the following error:
system('use Python-3.4')
sh: use: command not found
It seems like this is a problem with my PATH. I am using the correct shell:
system('echo $SHELL')
/bin/bash
My $PATH variable also looks good. However, when I create a script that essentially does the same thing:
load_py34.sh:
#!/bin/bash
source ~/.bashrc
use Python-3.4
and call this script through R, then it actually runs, but for some reason, it doesn't change my python version within R. (I have verified that this script works from the command line.)
> R
> system('python --version')
Python 2.7.1
> system('sh load_py34.sh')
Prepending: R-3.4 (ok)
> system('python --version')
Python 2.7.1
So I'm a little confused, but if anyone can help, I would really appreciate it.
Suggested fixes
When I combine them into a single command, I still have the same problem:
> system("sh load_py34.sh; python --version")
Prepending: Python-3.4 (already loaded)
Python 2.7.1
When I try calling bash directly, I still have a problem with the PATH:
> system("bash -c 'use Python-3.4; python --version'")
bash: use: command not found
Python 2.7.1
.bashrc is only loaded for interactive bash sessions.
"use" is a bash function that is imported in my .bashrc file. It sets
all of my path variables.
If set via export, the environment of the calling process will not be altered.
export [-fn] [name[=word]] ... The supplied names are marked for automatic export to the environment of subsequently executed commands. (https://man7.org/linux/man-pages/man1/bash.1.html)
Child processes do not normally have access to the parent process' environment. (This poses a problem because system() creates a sub-process.)
The source and . built-ins execute the commands in the current shell environment, hence why your script works.
Other commands (executables, non-shell-builtins) are executed by the fork-and-exec mechanism, whereby the executing shell process forks, creating a child process with an identical environment and state. This new child process is the process in which the command is executed. Changes to the environment of that process are not replicated to the parent's environment.
This means that you will not be able to rely on system('...') to modify the environment of the R process, or that of processes spawned by subsequent system() invocations.
In a single invocation to system(), you can construct a command-line that changes the environment of the spawned shell like so:
bash -c 'source ~/.bashrc; use Python-3.4; python --version'
Mind you, ~/.bashrc is not really the best place to put this functionality (might be subjective).
When you call system() it uses /bin/sh, not /bin/bash. sh doesn't read your .bashrc file when it starts up, so it does not know any of the functions you've defined there.
To use the function from your .bashrc, you must get bash to run it instead:
system("bash -c 'use Python-3.4; python --version'")
Edit: placement of closing single quote.
I'm trying to use gud-pdb for Python debugging within Emacs.
Having an issue that pdb doesn't seem to be searching the PATH when looking for my .py files
I.e., I have a python script in a dir which is on the PATH, I can run this script from anywhere outside of pdb, i.e., from the command line.
But when I try and run it from within pdb it tells me the file doesn't exist.
I'm trying to run pdb against the script in a dir that contains the data to be processed.
I think this is a standard thing to want to do. I successfully do it for gdb and C programs all the time.
Anyone had this issue and know how to fix it?
Given that you're able to run your script outside of Emacs, but not
within, you probably
need
exec-path-from-shell.
This syncs up environment variables (like PATH) between your shell
and Emacs.
Have you tried the realgud package since you are using python?
;;M-x load-library realgud python -m pdb myscript.py
(package-install 'realgud) ;; python debugging in emacs
(defun sdev/init-realgud
(interactive)
(load-library "realgud"))
(sdev/init-realgud 1)
Dear fellow developers,
I'm repeatedly using (and developing) a python script for calculations, by executing it through the windows command prompt in each test.
The script has some parsed options.
In order to make each of my calculations easily reproducible, I save the actual command I entered to execute each calculation. For the moment I simply copy by hand the command once I executed it and I put it in a file. But since I have to do it for each calculation, I wonder is there is any python script line that could take my command line input, like:
python script.py --option="foo"
into a file.
The form of the command could be:
%save file=_command_used.txt% python script.py --option=foo
which would create the file and save the actual command "python script.py --option=foo" into it.
Thanks in advance!
Best regards!
I would love to have solutions for both Windows command prompt and Linux shell command prompt.
On Linux there is the script command that will capture all entered commands in a file. Use it like that:
script -a _command_used.txt
python script.py --option=foo
python script.py --option=bar
The -a option stands for append so the _command_used.txt will not be overwritten.
On Windows you can achieve a similar thing using Start-Transcript and Stop-Transcript cmdlet. See this related post.
Since you are using Python, I recommend you investigate the Xonsh shell as one way to solve this. It is cross platform and is scripted with python.
I like testing my python files from command prompt instead of inside of PyCharm, which is what I use. I use Python 3.6, and after downloading Python 2.7, there was clearly some kind of conflict, It would no longer execute my python files directly in command prompt. I reverted everything back to normal, but it now uses "pythonw" to externally execute my file, instead of executing it right inside command prompt, how can I restore this?
Example:
C:Users\User\PycharmProjects(file-name)> main.py
(Executes through pythonw)
I'd like it to execute directly in command prompt as it used to by default, instead of it using something external. Thanks.
If you are using python on Windows you may just need to edit your path variable that executes Python from pythonw.exe to python3.exe (or whatever the version is that you expect it to execute). See below link:
http://pythoncentral.io/add-python-to-path-python-is-not-recognized-as-an-internal-or-external-command/