What is '$' in python? [duplicate] - python

This question already has answers here:
What does the $ mean when running commands?
(5 answers)
Closed 7 years ago.
As a beginner in Python, I'm reading a book written by Bill Lubanovic
I found something weird.
In that book, After saving simple code in test1.py, which is
print("This standalone program works!")
it says python can run it by typing in
$ python test1.py
However, whenever I try to use that, syntax error happens.
Although I know there are other methods like using exec() which I found in this website, I wanna know why book uses that method which doesn't work at least for me.

It means you need to type everything but the $ in the terminal.
python test1.py
It's just a convention though. Authors also use > python test1.py and other notations.
I don't know which version of his book you're reading, but he mentions it in this version.
In the example that follows, $ is a sample system prompt for you to type a command like python in the terminal window. We’ll use it for the code examples in this book, although your prompt might be different.

You are not supposed to enter the $.
The $ represents the shell/terminal prompt. This is the string of characters that appear in your terminal when it is waiting for input, although $ typically indicates some flavour of unix, e.g. linux.
Your terminal will probably use a different prompt, e.g.
[user#localhost ~]$
Or, if you are using a Windows terminal you might see :
C:\>
or
C:\WINDOWS>

Question was answered in following stackoverflow post:
What does the $ mean when running commands?
What does the $ mean when running commands?
As of now, Python does not implement $ in its syntax. So, it has nothing to do with Python.
Instead, what you are seeing is the terminal prompt of a Unix-based system (Mac, Linux, etc.)
So basically is terminal prompt and you should type in only: python test1.py without $ sign. another example is ~ when using oh-my-zsh.

Related

Shortcut for not having to type out `python3 program.py` when running python program [duplicate]

This question already has answers here:
How do I make a python script executable?
(6 answers)
Closed last year.
Say I have the program program.py in a non-root directory. Thus, in order to run the program, I have to type python3 program.py every time. Is there a shortcut that I can enable in my unix environment where I type a specific command for a specific program to run. I.e. for the above, I want to just be able to type program and have the program run.
I am not greatly familiar with unix but I believe this has something to do with adding something to your path which I am unfamiliar with this method. Any references and/or support is greatly appreciated!!
Thanks!
Add this to the top of your file.
#!/usr/bin/python3, or wherever your install is.
Then chmod +x your_file.py.

Creating a scikit-learn contribution package [duplicate]

This question already has answers here:
What does the $ mean when running commands?
(5 answers)
Closed 7 years ago.
As a beginner in Python, I'm reading a book written by Bill Lubanovic
I found something weird.
In that book, After saving simple code in test1.py, which is
print("This standalone program works!")
it says python can run it by typing in
$ python test1.py
However, whenever I try to use that, syntax error happens.
Although I know there are other methods like using exec() which I found in this website, I wanna know why book uses that method which doesn't work at least for me.
It means you need to type everything but the $ in the terminal.
python test1.py
It's just a convention though. Authors also use > python test1.py and other notations.
I don't know which version of his book you're reading, but he mentions it in this version.
In the example that follows, $ is a sample system prompt for you to type a command like python in the terminal window. We’ll use it for the code examples in this book, although your prompt might be different.
You are not supposed to enter the $.
The $ represents the shell/terminal prompt. This is the string of characters that appear in your terminal when it is waiting for input, although $ typically indicates some flavour of unix, e.g. linux.
Your terminal will probably use a different prompt, e.g.
[user#localhost ~]$
Or, if you are using a Windows terminal you might see :
C:\>
or
C:\WINDOWS>
Question was answered in following stackoverflow post:
What does the $ mean when running commands?
What does the $ mean when running commands?
As of now, Python does not implement $ in its syntax. So, it has nothing to do with Python.
Instead, what you are seeing is the terminal prompt of a Unix-based system (Mac, Linux, etc.)
So basically is terminal prompt and you should type in only: python test1.py without $ sign. another example is ~ when using oh-my-zsh.

How to change Linux terminal idle default version [duplicate]

This question already has answers here:
Python IDLE: Change Python Version
(6 answers)
Closed 4 years ago.
Please I installed Idle3. But when I launched it by typing Idle from the terminal, it loaded python3.6.6 by default. If I search Idle under applications
There are three options:
Idle
Idle using python 3.6.6
Idle using python 3.7.0
My question is: Is it possible to change the default idle that opens when I type Idle from terminal?
Thank you
This question is a possible duplicate of Python IDLE:Change Python Version`
But in any case. Here's what you can do:
In your terminal type
sudo nano /usr/bin/idle-python3.6.6
The first line should look something like this
/usr/bin/python3.6.6
Change the end of the line to the version you are looking for idle to open by defauly (3.7.0 in your case).
Hope that helps:) oh and welcome to stackoverflow
If you do:
which idle
You get the path which is used for running idle, e.g. /usr/bin/idle
I'm guessing a bit but I think that it might be a soft link that points to the 3.6.6 version of idle. If so, you can see where the soft link points by doing
ls -l /usr/bin/idle
That would return something like "/usr/bin/idle -> /usr/bin/idle3.6.6"
To update the soft link, use ln with -f:
ln -s -f /usr/bin/idle3.7.0 /usr/bin/idle
And that should hopefully update the link to use the later version. Do note that I have assumed the /usr/bin path for the executables, it might be different in your case.

Meaning of ${PYTHON:-python} in shell script and how to change

I am trying to export a Mercurial repo to GitHub using hg-fast-export and Github Bash for Windows. It choked on the line from mercurial import node because mercurial doesn't support Python 3.
I installed Python 2.7 and tried shebang lines (#! /Python27/python) and also alias python='c:/Python27/python'. That worked to make python --version report 2.7, but the hg-fast-export.sh still invokes Python 3 because it contains the line
PYTHON=${PYTHON:-python}
and that evaluates to Python 3.4.3.
Can you explain how to change this to use a different Python version and also what's going on with the syntax here? I couldn't really Google the meaning of ${} or :- in the shell. Comments on how likely my approach is to get this running on Windows could also be helpful.
Edit: Thanks for the explanations of :-. Since the parameter expansion was not needed, I guess the answer to my question was "You have to set PYTHON='c:/Python27/python' in the same line as the script for it to use that value." I expected it to be like PATH where you can set it independently for following lines to use.
The intent here is to allow an override to be passed in through the environment.
Thus, if you run at a POSIX shell:
$ PYTHON=python26 hg-fast-export ...
then in hg-fast-export will evaluate ${PYTHON:-python} to python26.
That syntax evaluates a variable, but provides a default:
$ foo=123
$ echo ${foo:-456}
123
$ echo ${bar:-456}
456
You could try to pass a modified $PYTHON to the script:
$ PYTHON=c:/Python27/python hg-fast-export.sh ...
This is a way to evaluate and modify text (parameter expansion). Consider this example:
$ PYTHON="/usr/bin/python --version"
$ ${PYTHON:-python}
Python 2.7.10
PYTHON is originally the path and command on how to evaluate the version.
${PYTHON:-python} evaluates and runs the former, but it was not empty so the colon dash is not needed
For a detailed breakdown see What does the colon dash ":-" mean in bash

How to use Python3.2 with Dreampie? [UBUNTU]

I have a question.
I try to learn Python and of all programmer's tools I found Dreampie the most suitable. But, my dreampie always automatically sets Python 2.7. I found this topic and following the instructions, I changed both subp_main.py and subp_main.pyc (based on the first). Yet, it's still Python 2.7.
Telling truth, I suppose there should be some way to choose between the two versions, but I did not found anything in the Net or in program options. According to this topic writing "dreampie python3" inside the program should work, but dreampie inform me that the syntax is invalid (?).
[I quess the solution is very easy, but none of searchable things would help...]
You need to open a Terminal window (or Run dialog Alt+F2) and type in the command as follows and press Enter:
dreampie python3
It's not python syntax but shell syntax so you are running the dreampie application with python3 as argument.

Categories