Opening Python code with a specific version [duplicate] - python

This question already has answers here:
Multiple Python versions on the same machine?
(11 answers)
Closed 3 years ago.
while there are multiple versions of Python installed on a Linux machine, is there a way to mention in the script to be open with a specific version, say, 3.8 even if we issue the #python script.py as opposed to python3.8 script.py ?
I don't want to use Linux alias command. I wanna know if that is possible to be accomplished within the script

Use shebang. #!/usr/bin/python
In the first line of the code with the serial number of Python you want at the end.
You'll need to then run your script like ./my_script.py

You can select the interpreter by adding
#!/usr/bin/env pythonXX
as the first line in your script, provided the version is in the path.
You can also invoke the interpreter directly with the script as the argument.
pythonXX script.py
Depending on your situation
pythonXX -E script.py
might be better. That way the interpreter ignores paths set by environmental variables.
For more details view
https://docs.python.org/3/using/cmdline.html

Related

How do we create alias from python program/script? [duplicate]

This question already has an answer here:
create unix alias using a python3 script
(1 answer)
Closed 2 years ago.
I want to create an alias' through python program. I intend to use python as a replacement for shell scripting.
What I tried is:
import os
os.system('alias go2dir="cd /i/want/to/goto/this/dir"')
...and it does not work. I know the reason - that system command 'alias...' is getting executed in another shell and not in the current one where this python script is executed. So, that alias is not available to this shell.
What I don't know is - (In general,) how do we execute a command from a python program in the same shell where this python program is being executed. So that (in this case) the alias is available till the shell terminal is open?
The way other applications that want to automate actions in the user's shell work is that they write shell commands to their standard output. Then you can execute them with eval.
makealias.py:
print('alias go2dir="cd /i/want/to/goto/this/dir"')
Then in bash:
eval "$(python makealias.py)"
An example of a standard Unix program that works like this is tset with the -s option.

unable to source bash file from python shell script [duplicate]

This question already has answers here:
Calling the "source" command from subprocess.Popen
(9 answers)
Closed 5 years ago.
Hello I am trying to source of file say check.setup from my pythons script.
Code
import os
os.system("source /fullpathhere/check.setup")
It says command not found. Surprisingly I can source the same file directly from the shell.
check.setup is csh file. It is sourcing other .csh files and setting few environment variable I saw few answers here but no one could possibly solve the problem.
PS: I tried to write bash file instead of python. I also tried using subprocess.Popen. Problem persists.
Aashish
use popen
subprocess.Popen('source ./fullpathhere/check.setup', shell=True)
Why didn't tag your posting as csh, even though you explicitly mention csh in your question....
There is no executable namend source (you can verify it by typing bin/which source. Well, you pointed out by yourself, that this is supposed to be done by csh, but how should Python know that it needs to invoke csh, if you don't tell it?
I don't have a csh on my system, but if I remember right from the time a couple of decades ago, where I indeed used csh for programming, you can do something like
os.system("csh -c fullpathhere/check.setup")
Actually, I would also specify the -f flag for skipping .cshrc, unless you really need that to be sourced as well.

Python - Code does not run without #!/usr/bin/python [duplicate]

This question already has answers here:
Why do people write #!/usr/bin/env python on the first line of a Python script?
(22 answers)
Closed 7 years ago.
Okay I am new to Python, but my code does not run if the line
#!/usr/bin/python
is not present at the beginning of the file. Why is that ? What does it mean ? I thought it was used for to denote a python version if there were multiple versions installed.
#!/usr/bin/python
def main():
a = [1,2,3]
print a
if __name__ == "__main__":
main()
Omitting the #!/usr/bin/python gives the following error only if I execute it using
./test.py on Ubuntu
if however I use the python command to run then it runs fine without the /usr/bin line.
The first line of the script beginning with #! is a shebang (sometimes called a hash-bang).
The following executable path denotes which interpreter should be used to process the following code (in your case, /usr/bin/python).
If you run the script from the shell with python test.py, you don't need a shebang - the executable is python and the script is passed to it as an argument.
In Unix you can tell a file how it should be opened if it contains a script language (in your case Python).
This line is known as Shebang.
./filename is used to run executable files, to execute it you need to specify the application required.
Whereas, in using python filename.py you already specify the application to use that is python in this case.

Python startup script [duplicate]

This question already has answers here:
Passing options to Python executable in non-interactive mode
(4 answers)
Closed 7 years ago.
I would like to execute a script work.py in Python, after executing some initialization script init.py.
If I were looking for an interactive session, executing python -i init.py or setting PYTHONSTARTUP=/path/to/init.py would do the trick, but I am looking to execute another script.
Since this is a generic case which occurs often (init.py sets environment and so is the same all of the time), I would highly prefer not referencing init.py from work.py. How could this be done? Would anything change if I needed this from a script instead of from the prompt?
Thank you very much.
More generally than in the accepted answer of C0deH4cker, this is discussed in the Python manual in Section 2.2.5 - Cusomization Modules. The basic idea is, to get the location of the special start-up script, one needs to execute the following Python code, e.g. from the interactive session of the interpreter:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'
The output should be exactly such a location, in the file sitecustomize.py.
Python has a special script that is run on startup. On my platform it is located at /usr/lib/python2.5/site-packages/sitecustomize.py IIRC. So, you could either put init.py in that directory alongside a sitecustomize.py script that imports it, or just paste the content of init.py in the sitecustomize.py.

Is '#!/usr/bin/python' in front of every Python script a must? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do people write #!/usr/bin/env python on the first line of a Python script?
What does the line “#!/bin/sh” mean in a UNIX shell script?
It's a comment and should be ignored by the interpreter. Why is #!/usr/bin/python still in almost every program?
If the script is made executable, the operating system will use that first line to know which interpreter to run to parse the rest of the file to perform some action.
If you always run these kinds of scripts from the command line like python foo.py then you could remove the first line.
Not actually. It is tradition in Unix like operating systems to use the interpreter path given in the first line and use the path to that interpreter for interpreting the rest of the program. It is called as Shebang line. You would not cared for it if you were not on Unix like systems (linux. Mac OS, FreeBSD etc) but there are attempts in Python community to use similar shebang lines on windows too.
Read the Wikipedia article Shebang. Also remember outside the *nix world, the first shebang prefixed line is considered as a comment.

Categories

Resources