Force make to use python2? - python

I'm trying to build an Android kernel with make command. The script uses python2 to log errors in the print >> sys.stderr, line fashion. So instead of actual error output I get errors about incorrect python syntax.
I had an idea to delete python3, but I read that it could be harmful to the system as it comes bundled and many things might depend on it. Tried adding alias python=python2 at the end of the .bashrc file, rebooted the terminal, tested it with the line:
echo `python -c "print 'test'"` # prints 'test' correctly
But when I make, I still get the same errors, it somehow manages to use python3 anyways.
Where is my mistake?

You did not show your Makefile. The easiest thing is to define the absolute path of python on your system. You can get that by using the which command. Then, change python in Makefile to the absolute path that you found out using which python2.

Related

Python.exe works with -v or -h but will not execute a file. What is wrong?

The python programs on Windows have stopped executing scripts.
It will run python. For example, I can check the version and get the help text.
I've tried running it from "c:/User/.../python.exe", `py -3, etc.
I've also installed all Windows updates and re-installed python. (from version 3.8 to 3.10.7; same issue for both.)
Also, I've tried it from both power-shell and cmd.
I have tried a variety of scripts, but I have a test.py which just prints something.
Also, I've tried py -3 -c 'print("Hi")'. It does not work either.
All of these do not print any diagnostics. I simply get the shell prompt on the next line. I can also tell that the script is not even loaded. For example, if I give a filename that does not exist it does the same thing; no error message. The same occurs if I add a syntax error to the file.
I am seriously stumped. What could cause this?

How to run a Python script setting the location of the modules in the shell?

I would like to run a Python script setting in the shell where the interpreter must look for the modules.
Suppose that myscript.py contains only:
import mymodule ; mymodule.myfunction()
But mymodule is in /home/user/hello, whereas myscript.py is, say, in /home/user/Desktop. I want to run on a terminal something like:
$ python /home/user/Desktop/myscript.py LOCATION_OF_THE_MODULES=/home/user/hello.
Would it be possible? I think that an alternative solution is to define the location in the import statement from the code, but this is not what I am looking for. I want to set the location through a variable in the shell.
So, I've been exploring a little your question, turns out this isn't a Python question but a "prompt" question, because indeed there is a way to do that but, since Python can't hop into interactive from script, we can't make it using Python only, but the Python interactive command have some extra options we can use
see:
python3 -h
for more info.
Specifically there are 2 options that are interesting, -i and -c which stands for interactive mode and command string respectively, that way we can load the modules with -c and hop into interactive with -i, like so:
python3 -i -c "import os"
Obviously, we need to make it more advanced so it can load multiple modules without Python scripting, then we will be needing to make the actual command to run Python and load the scripts you want, there is a problem tho, since we need to issue a command to be able to load all the modules you want in a folder it might create incompatibilities with prompts since not all prompts have the same syntax. There might be another low-level answer to this problem but I couldn't get to it, however, I will leave a Bash Script for reference so you can use it and/or edit it so it works best with your prompt.
FINAL_VAR=""
cd $1
for f in *.py; do
FINAL_VAR+="import ${f%.py}"$'\n'
done
python3 -i -c "$FINAL_VAR"
Usage steps:
Copy and save the script
Give it run permissions (chmod +x file_name.sh)
Run it this way: ./file_name.sh "/full/path/to/your/modules"
It will load all the .py files and will hop into an interactive Python shell for your use
Note: You might want to change the last line so it works accordingly to your Python installation

Ubuntu Python shebang line not working

Unable to get shebang line working in Ubuntu for python script. I only get a command not found error each time.
test.py
#!/usr/bin/env python
print ('!')
Ran
:which python
/usr/bin/python
Played around with different locations for python in the shebang but no luck including what was provided by which python. Any tips on how to troubleshoot this?
Thanks
If you are trying to run the command as
$ test.py
the error may not have anything to do with the shebang. Rather, the directory that test.py resides in is not in your PATH. Try
$ ./test.py
to bypass PATH lookup.
(This is in addition to making sure that the script itself is executable.)
On the python docs page it says:
To easily use Python scripts on Unix, you need to make them
executable, e.g. with
$ chmod +x script and put an appropriate Shebang line at the top of
the script. A good choice is usually
#!/usr/bin/env python
which searches for the Python interpreter in the whole PATH. However,
some Unices may not have the env command, so you may need to hardcode
/usr/bin/python as the interpreter path.
I don't know if this applies for you or not.
Apart from executing the script with a preceding dot or making it executable, there might be another issue:
If you try to use a script written with a windows editor, it may contain windows line endings. Removing these can make the shebang work again.
To remove such line endings, refer to How to convert Windows end of line in Unix end of line (CR/LF to LF) for instance.
See also my general remarks on failed shebang evaluations at my other answer.
Make sure that the "FIRST LINE" is the shebang.
Do not give any newline character in the beginning of the file.
"No newline character in beginning"
This may be due to a kernel misconfiguration. Take a look at your kernel's config options, and check if CONFIG_BINFMT_SCRIPT is set:
zcat /proc/config.gz | grep CONFIG_BINFMT_SCRIPT
If the output of this command is anything besides CONFIG_BINFMT_SCRIPT=y, this means that your kernel will not allow you to use shebangs. You will need to get a new kernel or recompile your current kernel with CONFIG_BINFMT_SCRIPT=y.
You can also check your line ending in gitbash/pycharm terminal by typing,
cat .gitattributes
this will list the setup, for linux setup paste this
*.sh text eol=lf
if this doesnt work, then better create a new branch and delete the exisiting file and create newfile.
This worked for me

Executing python program

I have been searching the web for an answer now for quite a while, but this is giving me really headache:
I am using Ubuntu 12.04 and I want to execute a Python script from the terminal without using the full path.
So i added /home/kyril/python/scripts/ to the PATH variable through putting the following into ./bashrc:
kyrilpathvariable="/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/kyril/Python/scripts/:/home/kyril/Bash/scripts"
if [ "$kyrilpathvariable" = "$PATH" ]; then
echo PATH already exported
else
PATH=$PATH:/home/kyril/Python/scripts/
PATH=$PATH:/home/kyril/Bash/scripts/
export PATH
fi
(I know the if clause is not necessary but I did not like to have everything two times in my PATH if I type exec bash.)
Now the problem: this perfectly works for my Bash scripts, so after making them executable via chmod I can just type $ script.sh and it is executed. However if I type $ python3 script.py the following error is raised: python3: can't open file 'script.py': [Errno 2] No such file or directory
if I type in the full path to the script it works. Anybody has an idea what I am doing wrong? Do I have to add the directory to the PYTHONPATH? (As I understood this only helps for importing modules).
Thanks guys!
When invoking python3 directly, python runs the script file you told it to, without using $PATH to find it. PYTHONPATH is irrelevant--that's used for searching for Python modules.
I'm guessing you're having issues with the wrong interpreter getting invoked when you run script.py by itself. I don't know what the first line of your script is, but it should be this:
#!/usr/bin/env python3
Or if you need even finer control:
#!/usr/bin/env python3.2
And for Python 2 scripts:
#!/usr/bin/env python2
Or:
#!/usr/bin/env python2.7
You should check that these executables exist on your system before trying to use them.
I would guess that path variables are ignored when python searches for the input-file. Python starts searching for 'script.py' in the current directory, not knowing that there is a path variable declared for that file, and therefore cannot find it.
Unfortunately I'm not sure how to solve it but maybe someone more experienced with variables can enlighten us?
python3 $(type -P script.py)
Tells Bash to look in the PATH for the executable file and supply its location and name.
For example:
$ type -P script.py
/usr/local/bin/script.py
To avoid duplicate entries in the path, you can do:
for dir in Python Bash; do
dir_to_add="$HOME/$dir/scripts"
case ":$PATH:" in
*:"$dir_to_add":*) ;; # path already contains dir, do nothing
*) PATH+=":$dir_to_add" ;;
esac
done

Default Folder for Terminal in OSX, When Running Python Interactive Shell

This is a follow up question as I'm trying to move forward with Zelle's Python:Programming.
It appears that IDLE 2.7.2 has hang issues when opening graphics windows in interactive mode. But, I can simply run Python interactively in Terminal and don't have any of those issues. So that's a big help. Zelle provides a simplified graphics file called graphics.py, to get up to speed with objects and graphics, before dealing directly with Tkinter.
My question is this. Where should I put graphics.py, so Terminal will see it (when it's called), without including the full path every time? Thanks,
Henry
According to PEP 0370, a good place to put your user packages is in ~/.local/lib/python2.7/site-packages.
Modules in here should be importable.
You can modify your ~/.bashrc to include a PYTHONPATH definition that includes the location of the file.
export PYTHONPATH="/path/to/directory_containing_graphics.py"
Just be careful if it's in your home director to use /Users/[username]/etc/directory_containing_graphics.py, I vaguely recall being bitten by ~ not expanding like I expected one time on OS X.
I went deep down the rabbit hole to get to bottom of this issue. It turns out that bash on OSX has a few quirks, not unexpected with the various flavors of UNIX around. It turns out that when firing up a bash shell, bash looks in .bash_profile for config statements. If you enter bash into bash, you fire up a sub shell, and that's when and only when .bashrc is executed.
To make sure you only have to manage one config file, put:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
in your .bash_profile. This just tells bash to look into the .bashrc file if it exists. You can then leave that file alone and only modify your .bashrc file and all instances of bash will be modified the same. Once I figured this out, I still had to solve my problem. Here's the best I could do. When Python installed it installed this into my .bash_profile:
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
This tells bash where to look for the libraries needed to run Python. So I moved this statement to my .bashrc file after putting the "if" statement into my .bash_profile. I then tried adding a : to the PATH statement with the path to my Python modules, but that didn't work. So, I have settled for a statement like this in .bashrc:
PYTHONPATH=$PYTHONPATH:/Users/ftpbub/Documents/workspace/Python\ Programming\ Modules/src/
export PYTHONPATH
Now I enter python at the command prompt, and when I get to python, I can enter import modulename without the path and it works. If I start a new project in a new directory, I should be able to add it to my PYTHONPATH and be good. I would have liked to be able to enter python modulename right into bash and have the module execute, but I haven't figured out how to do that. Other than that, I think this the best I can do. Ideas for how to go straight to the module from the bash prompt, without entering the path?

Categories