Environment variable exported using bash script not accessible in python - python-3.x

I have two files :
run.sh
train.py
Content of both file
in run.sh
export TRAINING_DATA=test1
export FOLD=test2
python.exe train.py
in train.py
from os import environ
print(environ.get("TRAINING_DATA"), environ.get("FOLD"))
when I run the bash file through command line
bash run.sh
Instead of getting test1 & test2, I am getting None, None
Both the files are placed in the Scripts folder of my virtualenv. I am using Windows 10.

The issue you are hitting is that you are running the windows version of python.exe rather than the linux/bash version.
In the run.sh i see that you are refering to python.exe, this is the windows exe and as such when it runs it will not be in the same shell as the .sh script used to invoke it.
you can prove this to yourself by doing the following from within WSL
➜ which python.exe
/mnt/c/Python27/python.exe
➜ which python
/usr/bin/python
So as long as you have python installed in WSL you can simply swap out the .exe in the .sh script so that you are using the bash/linux version thus:
export TRAINING_DATA=test1
export FOLD=test2
python train.py

You should try disconnect and log back in on the session.
Happened to me and it's how I solved it.
On my side it was on a ssh console.
Have a great day/night-

Related

Is there a software that can make python scripts (.py files) on ubuntu 18.04 system execute like exe files on windows?

Is there a software that can make python scripts (.py files) on ubuntu 18.04 system execute like exe files on windows? Double click on ubuntu 18.04 and it will run
Adding the following line at the top of the script:
#!/usr/bin/env python
And making the script executable with chmod e.g: chmod 755 script.py on your command line should do the trick.
You could also use a program like pyinstaller to bundle the script into a linux executable.

I can't open a python file in git bash

I'm trying to open a .py file on git bash but it doesn't work.
I have tried to follow some instructions like running python <filename> but it doesn't work for me.
When I run
python python_basics
I expect it will open the .py file but it says it can't open file 'python_basics':
[Errno 2] No such file or directory
From this question, the problem may very well be caused by Git Bash itself.
I would recommend you try running your Python file from a different terminal (Command Prompt or PowerShell if you are using Windows), using the command suggested in the comments:
python python_basics.py
Thank you for your question, I am here to help you and who will see this question.
if you mean you want to open the file like when click on the file and open it
you can use this command
Start filename.py
but if you want to open the file inside the gitbash use this command
vim filename.py
and if you mean to run the file from gitbash you can use this command
python pythonFileName.py
Now if the above command did not work with you, and you are in the windows10 Pro platform you should go to
environment variable >> Then system variable >> then choose path >> then Edit >> and put the python path >> restart the terminal and run it again
Notice: All of the above I tried and used in windows10 pro.
Thanks,
Hope to help anyone,
First check the python version installed on your system.
by command-
python --version.
If not found
set
$ PATH=$PATH:/c/Python27/
Adapting the path will solve your problem.

Python3 GUI script does not work when double clicked

My GUI script that is a PyQt5 file (.pyw extension) does work when running on my IDE with a build configuration that tells the compiler to run the script with python3:
And it also works when i tell to the regular terminal on Linux to run same script with python3 like this:
When runned with the default python (python2.7) on a regular terminal it tells: ImportError: No module named PyQt5.QtWidgets.
My code does it have these lines on the start to tell that is a python3 script like: #!/usr/bin/python3 or #!/usr/bin/env python3 (I have python3 installed).
When double clicked on the Linux Mint File Explorer the cursor turns crosshair and nothing happends, with the terminal option, same happends and a empty terminal shows. Im talking these options
I guess Linux Mint still runs the scripts with python2.7 even when I added the bash lines to tell
Someone knows why the lines:
#!/usr/bin/python3
#!/usr/bin/env python3
doesnt work when just double click?
I want to run the script from the Linux File Explorer without the need of an IDE or using the terminal.
Try chmod +x file.py and run it in terminal by using ./file.py also try lunching the file from a different path, like python3 ~/path/to/file.py and see if the error persists

Python3 no such file or directory

I am trying to make python3 executable scripts and run them from shell.I have python 3.4.0 installed on my system.
So, I added '/home/spandan/python_codes' directory to PYTHONPATH, as I am planning to keep my scripts and modules here.
However, while trying to execute these, the above error is thrown by the system, and the scripts wont execute unless I go into the python_codes directory and then execute them.
Executing python program : Here I found out that PYTHONPATH is irrelevant while making scripts, and also how to set the python shebang. So I set mine as #!/usr/bin/env python3.4.0
Is it correct?
You don't have to put your python codes in a global path. Just make your python 3.4 interpreter interpreter available globally. For that, edit .bash_profile or .bashrc file in your home directory and add the following line:
export PATH=${PATH}:/usr/bin/python3
That will make python3 executable irrespective of your current working directory.
In order to execute code from your codes directory, you just have to write:
$ python3 ./your_code.py
Another way is to add the shebang at the top of your code as
#/usr/bin/python3
and change the permission to executable by the current user (by default it will not have execute permission).
$ chmod 744 your_code.py
and then executing the script directly as
$ your_code.py
I hope I could address your problem.
Another way to do it is to use python-is-python2 or python-is-python3 debian packages with /usr/bin/env python sheabang.
This option will let the end user select which interpreter he want to use while maintaining your code versionless and avoiding some people to install unwanted interpreter versions.
As an example, run this commands to make a sample file:
cat << EOF > version.py && chmod +x version.py
#!/usr/bin/env python
import sys
print(sys.version)
EOF
And run the following command if you want to set python2 as default:
sudo apt install python-is-python2
Or run this command if you want to set python3 as default:
sudo apt install python-is-python3
Finally you could see the python interpreter version that you have been selected by running:
./version.py
The only drawback of this solution is that you could make your python scripts compatible across python interpreter versions, but thats your decision!
You can install which you want and change from python2 to python3 and vice- versa as you want, but the idea is to fix a python interpreter version in a system and not change it again unless definitive upgrade.
References:
https://askubuntu.com/questions/1296790/python-is-python3-package-in-ubuntu-20-04-what-is-it-and-what-does-it-actually

How to run .sh file from python program Ubuntu

I am trying to launch a .sh script from Python 3.3 in Ubuntu 13.10.
The script is supposed to shutdown the computer. I have already marked the sh script as executable through the terminal. I have tried to run the sh script through: os.system("script.sh"), subprocess.Popen("Script.sh"), and subprocess.call([script.sh]).
They keep returning the: OSError Exec format error.
Any help would be greatly appriciated!
I assume that script.sh isn't in your PATH but in your current working directory.
By default os.system and subprocess look in your path for the requested executable. So to execute something in your current working directory you need to specify the executable like this:
subprocess.call("./script.sh")
The ./simply says that the executable that should be executed is in the current working directory.

Resources