using python 2.7 for windows executing an sh file - python

Windows 10.1 installed
python 2.7 installed
I have an application package that has a file named install.sh and the read me states to run the install.sh file. I have tried numerous times to execute the .sh file without success. I get a name error and that install is not defined. What is the proper syntax to execute the .sh file as directed in my read me?

You need to have a Bash shell installed to run .sh files. Easy options include Git Bash and Cygwin.

Related

Running Python File from Command Line with Libraries in venv

I'm using Python 3.6 in Windows and using PyCharm. I have a .py file that is using packages installed on a venv which is in a different folder to the .py file.
I'm trying to run this .py from command line and when I do it gives me a ModuleNotFoundError: No module named '<module>'. The file works fine from PyCharm just not from the command line because the packages are in the venv.
How can I get the file to run without errors from command line and keep the packages in the venv?
Many thanks.
You need to activate the virtual environment by callling the activation script:
<path to your environment>\Scripts\activate.bat
as indicated here. Then you will automatically use all the packages installed in this environment when calling your script. Your pycharm is probably set up to automatically use your virtual evnironment
I Think the easiest way to do that is using shebang, and it works both linux and windows.
For windows you just have to add #!.\venv\Scripts\python.exe at the very first line at your .py script file.
Or
source <path to your environment>/bin/activate
on linux

Python Import on Apache: File not found

I have a problem with my apache server (Shared Hosting, no root).
I've installed a python package via SSH-Terminal (with pip install) under ./local/lib/python2.7/site-packages/.
There is a python file which imports this package. When I run the python file via the SSH-Terminal everything works fine.
But when I run the python file within a php exec command
exec("/usr/bin/python myscript.py 2>&1", $out, $result);
an error occurs:
"ImportError: No module named XXX".
Do you know what went wrong here or what I can do to make the script work in the browser as well?
Thanks in advance

Different environment variables on git bash and windows cmd

I have a python file in which I'm using the subprocess module to execute some command line scripts.
I'm using Git bash to run this python file. In the file, I execute the script:
KG_URL=http://127.0.0.1:8900
This script sets the variable successfully when I run it manually on the git bash command line.
But when I execute this using the python file, it gives me the following error:
'KG_URL' is not recognized as an internal or external command,operable program or batch file.
I tried digging deeper into this and I found out that executing the python file on git bash is the equivalent of running those scripts on the Windows cmd. When I tried running the set command without parameters (to get the current environment variables) on the Windows cmd, I found out that the variable KG_URL does not exist. But when I ran the same command on git bash, I can see that KG_URL exists.
Any idea why this discrepancy exists? And how can I solve this issue?
The reason why I'm executing these scripts in a Python file is because I need to convert it into an exe later. Assuming that all environments where I run this exe will have git bash installed, is there any way of ensuring that these scripts run only through git bash, and not the Windows cmd?

Run a .py file without Python installed using .bat

I want to create a .bat file to run my .py file on Windows without Python installed, mainly so I can send my programs to friends. So in my .bat file I would like to:
Change to the current working directory. (So it runs wherever the file is)
Change directory to a folder inside that directory.
Run a python file without my friends having to install python themselves.
Run a .py file without Python installed using .bat
You can't run a Python script using a batch command without Python being installed. You can compile an executable with Py2exe (which bundles an entire Python interpreter with your script), or convert a subset of Python to C++ with shedskin, which can then be compiled to an executable. You could also issue a shell command to install Python if it's not already installed and the user has internet.
But doing exactly what you asked is impossible.
Take a look at http://www.py2exe.org/ which will convert you python code to a executable windows program that you can send to your friends.

Running python from the mac terminal

I have installed the new python release and would like to run .py files from the terminal.
How is this done from the terminal? I dont want to include the path in each command to run a .py file.
If you want to override the python command, you can set your PATH variable correctly, e.g. in your ~/.bash_profile:
export PATH=/path/to/python/:$PATH
That said, for managing different versions of components that are also provided by Mac OS X, I suggest to use a package manager such as Homebrew.
if you add a shebang at the start of the python file then you can run a python file by just its name from terminal
add #!/usr/bin/python
for mac(others add your respective path for python)
at the top of your python program and from your terminal you can run it just by filename(if it has executable permissions).
Have a look at the Python package under Applications. There is a shell script there called Update Shell Profile.command
Run this and it should set your path up properly.
Unless you mark you script as executable with chmod +x, you'll need to run python over it first. e.g. `python myscript.py'
I installed all of my python through macports, which has pros and cons. One of the benefits is that you don't have to worry about stuff like this, it just works. You can install python 2.6 and python 2.7 (and others), and then use the python_select utility to set up which python is run when you call "python blah.py"
Since you have installed a working python, the easiest way to run python files from the terminal is to cd your terminal to the directory where the file is located and then just type python my_code.py in the terminal.

Categories

Resources