Run Python Script with Bash modules in Windows - python

I have a Python script and I'm trying to run it from Windows or some online Python interpreter. I would like my friend to test it but he doesn't have linux.
The conflict here is that I have modules such as "import os" in the script that is only native to the bash environment. So how can I get my friend to run my .py script in Windows?
Thanks!

The cool thing about python is that you friend should be able to run your script with little to no change(hopefully), to quote the python os docs
This module provides a portable way of using operating system
dependent functionality.
If your friend did have an issue you might have a another question to ask.

Related

Blender as Python module issues to use

For test purposes,
I tried to install Blender as a Python module in a virtual environment with Python 3.7 to be sure that i can use bpy.
I follow this page for the installation (https://wiki.blender.org/wiki/Building_Blender/Other/BlenderAsPyModule)
I would like at the end, when I wrote a Python script who used blender command to be able to run outside Blender software and to get information when my script finish
So I have multiples questions:
I don't really understand this line of command for Windows and how I can adapt to my virtual environment :
xcopy /E bin\2.81 C:\Python37\2.81
I can't use 'import bpy' command even when I successfully do pip install bpy in the command line ?
For my final goal project, I'm afraid that it's impossible to do because I couldn't find examples or questions related to my project.
Can someone could explain a little if it's possible or interesting to use Blender externally ?

How to run python programs in Windows 7?

I downloaded this tool to migrate MySQL to PostgreSQL: https://github.com/philipsoutham/py-mysql2pgsql
Python interactive code works properly so the python path is set in the right way.
When I type "py-mysql2pgsql" being in the directory: C:\Users\me, the downloaded tool doesn't run but ask me to choose the program to open that file. The same situation when I'm in C:\Users\me\py-mysql2pgsql
How can I run this tool properly?
Windows does not understand shebang lines in scripts (#!/usr/bin/env python) like Linux and Unix variants do. So Windows does not understand that this is a python script, you need to execute python yourself.
If python executable is in your path, you should be able to run:
python py-mysql2pgsql
If it is not in your path, you should be able to run:
path_to_python\python py-mysql2pgsql (on my machine C:\Python27\python)
Note that this applies to any python script on Windows, not just this tool.

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.

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.

How can I run a python script on windows?

Can anyone please tell me an IDE for running python programs? Is it possible to run the program through command line?
Take a look at ActiveState's ActivePython. It's quite a nice implementation of Python on Windows. Another way is using Cygwin's Python port. These two are Python implementations. I don't use an IDE, I write my Python code in Notepad++.
To run a python program after saving it to C:\Users\vaibhav\Code\myscript.py:
ActivePython: If I remember right, ActiveState updates the path correctly. So it should be a s simple as:
Press "start" in the task bar
In the search field search for "cmd"
In the appearing box navigate to your folder with the python script: dir Users\vaibhav\Code
call python myscript.py and you're done
Cygwin: After installing Cygwin, you have a full-featured bash terminal on your Windows machine.
click on the Cygwin icon on your desktop
In the appearing window navigate to the folder with your python script: cd /cygdrive/c/Users/vaibhav/Code
type python myscript.py
e voila
IDE for running scripts? You can have any IDE you like, but if you need only to run python scripts you go like this:
python.exe pythonScript.py
I like the EasyEclipse for python distribution. You'd need to have python and java installed of course.
PyDev and Komodo Edit are 2 nice Python IDE on Windows.
I also like the SciTE text editor very much.
These 3 solutions make possible to run Python scripts
I tried to run a Python script with multiprocessing on windows. see this tutorial
It does not work on Windows, but on raspian it went very well. Thus I knew that it was a Windows problem. I installed cygwin and followed this tutorial Installing Python inside Cygwin.
After that I additionally installed numpy with the command easy_install numpy and now i can run python scripts with multiprocessing on windows (from cygwin).

Categories