(New to Python)
I've downloaded a project from Github which contains an index.html file. When I bring the file to my web browser I get some errors that tell me I can't run js libraries.
In the Python shell, I'm trying to setup the HTTPSimpleServer using this command:
python -m SimpleHTTPServer
but I get this error:
File "<stdn>", line 1
python -m SimpleHTTPServer
^
SyntaxError: invalid syntax
I'm running Python 3.6.
I've also tried with
python3 -m http.server
and it produces the same error.
Am I missing a library? Don't see any syntax issues.
You need to type
python -m SimpleHTTPServer
at your command prompt, not inside the python interpreter. That is a python error message and the line above is a command line for your OS.
Related
I know that I can run .py file in terminal by using command line:
python module_name.py
I can also use
python -m module_name
Both can run a module. So, I wanted to that what's the between difference both command line ?
I tried to open a file named (choony.py) from the terminal and I got an error.So,
this is what I typed in the terminal:
python3 choony.py
This is the error that I got
No such file or directory
I tried this in the terminal
echo $PATH
This is the result that I got
/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
As you can see, python is running 3.7
how can I make python 3.8 the defult.
I am trying to run the following script "test.py" on Anaconda Prompt:
from tensorflow.keras.applications.resnet50 import ResNet50
...with the following command:
(conda_env) C:\dev>test.py
This results in the following error:
ModulNotFoundError: No module named 'tensorflow'
When I run the same script on Anaconda Prompt with the following command I don't get any errors:
(conda_env) C:\dev>python test.py
I have installed tensorflow in the Anaconda environment 'conda_env'
(conda_env) C:\dev\>conda env list
# conda environments:
#
base C:\Users\xx\Anaconda3
conda_env * C:\Users\xx\Anaconda3\envs\conda_env
keras_1 C:\Users\xx\Anaconda3\envs\keras_1
tf-gpu C:\Users\xx\Anaconda3\envs\tf-gpu
Why is this like that?
You won’t get errors if you do
(conda_env) C:\dev> python test.py
because then you’re following the correct syntax for running python scripts in a terminal. By adding python before the .py file, you initiate the Python interpreter that executes your script. Without it, the terminal won’t know what Python interpreter to use to execute your script and may end up using an interpreter that doesn't have the modules you require. There are ways to skip writing python before executing if that’s what you want.
For example, see: Calling a python script from command line without typing "python" first
my current bash shell is loaded with python3 but occasionally I tend to run old python2 scripts and I get this error
python/2.7.10/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory"
so every-time I had to load python2 back and forth. Is there a way to run python2 script in python3 environment by just changing any environmental variables?
Are you open to virtual envs? Otherwise you can run python2 or python3 from the command line.
As an example, create the following file, hello_world.py:
#!/usr/bin/env python
print "Hello, World!"
Then you can run from the bash shell:
python2 hello_world.py
That should work no problem. However, if you run:
python3 hello_world.py
You will run into the error:
SyntaxError: Missing parentheses in call to 'print'. Did you mean
print("Hello, world!")?
I am running a basic Python file on Windows XP from IDLE.
The file name is assignment1.py.
The file content is:
import sys
var = 5
but when I run it, it gives the error:
Command: python assignment1.py
SyntaxError: invalid syntax
Then I tried another thing which also gave an error:
Command: which python
SyntaxError: invalid syntax
Not sure if the installation is wrong or something.
I am able to run the print command successfully:
>>> print "I am working fine"
I am working fine
Not sure of the issue. Request help.
It looks like what you are entering as the "command" is being interpreted as Python code. I mean, python assignment1.py is interpreted as Python code. The result is as expected:
$ python -c 'python assignment1.py'
File "<string>", line 1
python assignment1.py
^
SyntaxError: invalid syntax
You need to run the file in the correct way, probably via the IDLE menu or by pressing F5. You can check these questions for details:
How do I run a Python program?
Running Python script from IDLE on Windows 7 64 bit