How can I execute command line program with ./ via a Python script? - python

I have a program in a directory ~/trevor. Using Ubuntu, if I cd to this directory then I can execute this program with
$./program
However, I want to be able to execute this program from within a Python script, but when I try using
import subprocess.run
subprocess.run("./program")
I get the following error:
OSError: [WinError 193] %1 is not a valid Win32 application
How can I solve this?
I am running the python script through Pycharm's terminal. It has occurred to me that I could run the python script through Ubuntu's terminal, but then I have a problem with Ubuntu finding the correct packages. I am running Windows with a Ubuntu shell - I tried solving the Ubuntu package problem by changing Ubuntu's PYTHONPATH to the same path as Pycharm's project interpreter, but this didn't work either. I thought it would be easier to solve the problem above than to the package problem.
Thanks for the help.

As previously discussed in comments, and then included in an edit to the question:
The mistake was trying execute a Ubuntu program through a Windows command line. The solution here was to execute the Python script (with subprocess.run() or subprocess.call()) through the Ubuntu command line.
Insofar as executing Python on Ubuntu can't find Python packages/modules, this is because those packages/modules were installed through the Windows command line, so native/compiled dependencies they included were built for Windows rather than Ubuntu. Even if Ubuntu's PYTHONPATH is pointed to the location of the packages, there are some modules that won't work because they have platform-native components. The solution is to download the package again but through Ubunutu (such that they are suitable for a Linux distribution).

os.exec* typically works for both windows and linux as far as I'm aware, however it does require an a letter or two that replace the "*" in certain situations. You'll have to look at this to see which one you would need https://docs.python.org/2/library/os.html
ex. os.execl or os.execle
subprocess.call is an older version that will avoid a lot of the problems with shell specific conventions. If the issue is Ubuntu-nativity as the comment above suggests, this could fix it.

Related

shebang not working on WSL (Windows Subsystem Linux)

I installed WSL (Ubuntu 18.04) on Windows 10 to use instead of using parallel 2 OS. However, when I was using shebang, I couldn't run the script named test_file.py successfully. First, I created a folder named test_project. In this folder, I created:
An src folder that contains a simple Python script is shown as follows
#!/usr/bin/env python3
print("Hello world")
A Python environment folder named environment through python3 -m venv environment/ (I'm using Python 3.6)
The problem is when I stepped into the environment and typed some commands like in the attached picture Run python file, the program ran "command not found" although I already used shebang at the beginning of the code.
Apart from that, I also changed #!/usr/bin/env to #!/usr/bin/python3 and escape the environment, it didn't work as well. Only when I type Python3 test_file.py it worked.
Has anyone faced this problem before? Can anyone explain to me why this happens? I'm wondering whether it is different between using WSL compared to Ubuntu in this case, or I was missing some steps during coding. I know that this problem is debated common previously, however I couldn't find any source that can tackle my problem. I really appreciate all your help.
I had the same issue and resolved it by the changing the line endings from CRLF which is native to Windows to LF which is what the shell is expecting.
You can do this with the dos2unix command:
dos2unix -b test_file.py
You'll probably need to install dos2unix on some newer distributions.
There are other methods that can be used as well.

Issue with Python on Windows 10

Specifically on my shiny new Surface Pro 2017 (i5, 256GB, 8GB RAM). So I'm trying to set up my dev environment and I'm running into a bit of a snag.
It shows up as installed through Powershell -and I'm able to run Python fine through it- but when I try the same on my Bash and Hyper terminals, no luck. I'm realizing now that Bash is not pictured in the screenshot, but it says the same thing when I try to run python commands. Did my due diligence in searching through the Surface subreddit, Google, and of course, Stack.
Anyone know what could be the issue?
Screenshot
In my opinion the best solution is to install python inside your bash environment, too. What is the main reason you even want to call the Windows version of python from the bash? If installing python inside the Ubuntu subsystem/the bash again, then you might try to find out the path to the python.exe inside your Windows installation and try to add these path to your bash's path, too.
Do yourself a huge favor and install Anaconda, and use command line prompt from now on and don't even Touch Powershell or bash. It's windows not linux. The reason I say anaconda is because it gives you a huge array of libraries and also you can create a virtual environment meaning if Python gets screwed on or corrupted at least your file paths will be okay.

Run Python script in a custom Python folder

I have a problem running a python script on a system that doesn't have Python installed. I know what you're thinking...but hear me out.
Some applications like C4D and Maya come with their own versions of Python. Unfortunately, they often compile them incorrectly, so modules that should import on their version of Python (e.g. 2.6 for C4D) don't work at all. I don't know why they do this, I've asked, but it appears to be due to a lack of knowledge on their part.
To use a module that won't import, you have to use a separate python installation. But I don't want to force users to install python, so I include my own python folder (2.7.6) with the modules I want to use inside and launch my script inside my custom (non-installed) python folder like this:
cmd = [my_python_path, "-E", my_script.py]
p = subprocess.Popen(cmd, shell=False, bufsize=...etc.
This works fine as long as Python 2.7.6 is actually installed on the system, but if it isn't installed, then it doesn't work. My system above isn't targeting, or using the installed python. In fact, I've moved the installed Python folder, and renamed it to make sure it isn't being used somehow, and my script works fine. So I know it is executing with my python folder.
Question 1: Why won't the python.exe run inside my custom folder unless there is an installed version of python? Is this because of some path variable?
Question 2: How can I make my python.exe work on systems, both Mac and Win, without Python officially installed?
Thanks
Just running Python.exe from custom folder doesn't tell it the specific location of many files and folders. These stuff are fixed in Windows as System Variables.
If you are copying the whole python folder, why not install python instead? It'll take same space too? Or if you really wan't to create executable use:-
Py2Exe for windows!
py2app from Mac!
Pyinstaller for both
I prefer py2exe.

python Bad Interpreter error

I am trying to install Sublime Text 2 on my linux machine and I cannot figure out how to run the python script to install it. I am fairly new to linux and never programmed in python before. I am trying to run the python script PackageSetup.py using ./PackageSetup.py but i get the error:
bash: ./PackageSetup.py: python: bad interpreter: No such file or directory
not sure what I have to do. I have python on my machine. I can tell cause running 'python' puts me in the console.
For all I know this kind of issue can occur if you have a misspelling or mistake in the shebang. I see two ways to solve this issue: you can try to invoke PackageSetup.py via python, like python PackageSetup.py or you can open PackageSetup.py and try to find an issue in the shebang, maybe it needs to be a full path to python interpreter like /usr/bin/python not just python.
BTW, why don't you try to install it via some kind of package manager - there is a repo for debian-based distros, and I'm pretty sure that there have to be repositories for other distributives.

Need to run python script in an environment where python is not present

I need to run python in an environment where there wont be python. Is it possible to execute python as an executable in Unix Environments, like HP-UX, IBM-AIX, Solaris, Linux etc etc....
The targeted OS is AIX now.... since it does not have python support and the installation is difficult......
Thanks.
I have used http://www.pyinstaller.org/ to create an executable in ubuntu. look at their manual, it also have the cool feature of outputting just one file with --onefile. My first choice was freeze but the executable failed to run when I used some external modules - I could not solve it and I found pyinstaller to be perfect for me.

Categories