I have a sample.py python file, which contains this line:
python -c "import numpy"
Run Command:
$ python sample.py
When executed, the script got the error:
SyntaxError: invalid syntax
Can anyone help me what is the issue with this line?
The content is not python, it's a command.
The content of sample.py should be just
import numpy
python -c ... calls python from a shell, and executes -c's argument by python. If you want that in a script, use a shell script, not a python script.
Alternatively, if you're running it with python anyway, drop the python -c, and just keep the python code (import numpy).
Related
I am trying to execute a few commands using python shell to import a module ABC. For that I have created a python file test.py having the following lines:
import os
os.system('python')
os.system('import ABC')
Now I am trying to execute that file as follows - python test.py. Here, it runs till the 2nd line i.e. os.system('python') properly. Then when it enters into the python prompt and there it remains stuck as it is not command prompt any more but python prompt. So how to execute the next command of importing the module ABC on the python prompt?
Command line and environment (Python Documentation)
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
In your terminal, when you run python followed by the name of a script (test.py), you are executing the contents of the script. Within that script, when you call 'python' using os.system('python'), you are starting a new interactive session, similar to if you were to just call 'python' from your terminal.
import os
os.system('python -c "import ABC"')
You can use the -c argument with python.
For example,
import os
os.system('python -c "import ABC"')
It sounds like what you need is to put the commands you require in a Python script, e.g. my_imports.py:
import ABC
import my_module
And run it from interactive Python with something like:
exec(open('my_imports.py').read())
That will execute the code in the script in the current context, making the imports available to you on the Python CLI.
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 want to print python's sys.path from the command line, but this one is not working:
python -m sys -c "print (sys.path)"
although print -c "import sys; print (sys.path)" would work. It seems that "-m" in the first one above does not load the module "sys". Any clarification on how to correctly import a module from python flags? Thanks.
There is no such flag. -m does something completely different from what you want. You can go through the Python command line docs to see the lack of such a flag if you want.
Just put the import in the command.
python -c "import sys; print (sys.path)"
(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.
I'm trying to run a one line code on command line via Python.
For example this one is working;
py -c "import time;a=lambda x: [x for x in range(10)];print(a(5));time.sleep(2);print(a(15))"
I got the expected output with no error etc.
But when I try to open for example Notepad.exe via subprocess;
py -c "import subprocess;lul='Notepad.exe';subprocess.call([lul])"
It raises error
SyntaxError: can't assign to function call
What's the problem here that I can't see? Also I tried to write Notepad.exe directly into subprocess instead of making a variable, same error.