Need to set up a local server to test out my webpage.But the command is not working and does not give any error message.I can't access http://localhost:8000/ too.
As outlined in the comments, it looks like your PATH variable is mixed up between the commands python (normally used for Python 2) and python3. You can see that python is actually pointing to Python 3 based on your result of python -V, so I would just go with that.
If you want to use python3 as the command for Python 3, the answer depends on how you honestly set up python to be Python 3, but some tips would be:
Reinstall Python 3
Create an alias in your shell profile (alias python3="python")
Related
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
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.
I am running a python program on a server, and on my account on the server I have the python version set in .bashrc file as follows:
alias python="python2.7"
I have a python script that I would like to be able restart itself. It works fine locally, but when I restart it on the server, it works once, and then switches to a different version of python. I have the following function:
def restartScript(self):
print("Restarting server")
print(sys.executable,['python']+sys.argv)
os.execv(sys.executable,['python']+sys.argv)
The first time I try to restart it prints the following:
/usr/local/bin/python2.7 ['python', 'server.py']
However the second time I run the server, it prints the following:
/usr/local/bin/python ['python', 'server.py']
This also gives an error, because I am using a module that is installed for /usr/local/bin/python2.7 but isn't installed for /usr/local/bin/python.
Is there an easy way to make sure that the server always restarts with /usr/local/bin/python2.7? I would like to make it flexible so that someone can use this restart whether they have defined their default version of python in .bashrc or are using a virtual environment. Also would like it work if they are using python 3 or python 2.
The following works independent of which python version you are using:
os.execv(sys.executable,[sys.executable.split("/")[-1]]+sys.argv)
I feel like I may be misunderstanding something, but I think you just want to pass 'python2.7' as the first element of your list of arguments instead of 'python'. Or if you're not sure what the executable name will be, pass sys.executable.
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
This question has probably been asked to death, but I can't seem to find the answer.
I can run python 3 script like this:
python3 my.py
However, suppose I make it executable and run it on its own. Then it typically executes under python 2, but I want it under python 3. Is this possible?
chmod u+x my.py
./my.py #executes under python 2.
Can I add something to the first line of the script? Or can I change global system default? (I'm on OS X).
Also, is it safe to change global default, will any of my applications stop working?
Almost all *nix variants support the following:
#!/usr/bin/env python3
So adding it as the first line will find Python 3 assuming the path where python resides is including in the $PATH
I think that if you write
#!/usr/local/bin/python3
as the first line of the file, it will be executed b python 3.
Note that you must execute it as:
./my.py
if you do
python my.py
I have no idea what might happen. If your default python version is 2.7, it will probably be executed by that version.