I am trying to learn the Stackless python and I got the latest 3.3.6. I created a try.py file and wrote the content
print " Hello world"
print " Good evening"
Then in the python command prompt, gave the command
import try.py
It gave the error ,
Syntax error: invalid syntax as shown in the figure below:
What's wrong here? I have placed the file in C:\Python33.
This is happening because try is a reserved keyword in python.
Try again after renaming your file to something else.
Also, during importing, specifying the extension of the python file is wrong.
Instead of import mymodule.py, you should simply write import mymodule.
Related
I have installed a package named 'Spexxy' with python3 setup.py install which worked fine.
Now, I am trying to use it, so with ipython3 I import the package as follow import spexxy which also works fine, but when I try to use the different tools available in this package, I keep getting the error invalid syntax while I am following the documentation of the package, for example:
I want to creat a grid as specified in the documentation, using spexxytools grid create, here is what I do:
spexxytools grid create --from-filename "lte(?P<Teff>\d{5})-(?P<logg>\d\.\d\d)(?P<FeH>[+-]\d\.\d)(\.Alpha=(?P<Alpha>[+-]\d\.\d\d))?\.PHOENIX"
I get the error:
File "<ipython-input-17-e08367969322>", line 1
spexxytools grid create --from-filename "lte(?P<Teff>\d{5})-(?P<logg>\d\.\d\d)(?P<FeH>[+-]\d\.\d)(\.Alpha=(?P<Alpha>[+-]\d\.\d\d))?\.PHOENIX" .
^
SyntaxError: invalid syntax
The documentation is present here: https://spexxy.readthedocs.io/en/2.1/
Can you tell me why ? Thanks
That's shell syntax. Call it from IPython by prepending an exclamation mark:
!spexxytools grid create --from-filename ...
Based on the documentation, the output will be a file called grid.csv.
If you actually want to use spexxy's Python API, see the API Reference
the inside brackets within a string keep turning red and I think its giving me a syntax error, specifically the first print statement when I run it in the terminal command prompt.
print(f"Hi {user_name}, Im the {script} script")
^
SyntaxError: invalid syntax
Ive tried googling it, replacing brackets(trial and error) and tried youtube, thought this would work better
from sys import argv
script, user_name=argv
prompt="> "
print(f"Hi {user_name}, im the {script} script")
print("Id like to ask you a few questions")
print(f"do you like me {user_name}?")
likes=input(prompt)
print(f"where do you live {user_name}?")
lives=input(prompt)
print("what kind of computer do you have?")
computer=input(prompt)
print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice
""")
You would not be getting that syntax error on Python 3.7. Most likely, you are attempting to run the script using an old version of Python that does not have the new f-string syntax.
On macOS, be aware that by default, the python command refers to Python version 2. Try running the script with python3 instead.
If you just type in python at the terminal, the banner before the >>> prompt will tell you exactly what version it is. Same with python3.
Note that you must have at least version 3.6 to use f-strings.
I think you only run into an error when you attempt to enter a name more than a word. So I made some adjustments. See if this was the case:
from sys import argv
script = argv[0]
if len(argv) < 2:
print("you must give me a name as commandline param to work with")
exit(0)
user_name=' '.join(argv[1:])
prompt="> "
print(f"Hi {user_name}, im the {script} script")
print("Id like to ask you a few questions")
print(f"do you like me {user_name}?")
likes=input(prompt)
print(f"where do you live {user_name}?")
lives=input(prompt)
print("what kind of computer do you have?")
computer=input(prompt)
print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice
""")
I'm a beginner of Python and I want to run a python3 script in PyCharm Python Console, but it raise an error below:
>>> /Users/mymac/Documents/Python/test.py
File "<input>", line1
/Users/mymac/Documents/Python/test.py
^
SyntaxError: invalid syntax
I don't know what's wrong the file path is, how can I solve the problem?
use execfile('/Users/mymac/Documents/Python/test.py'). You are trying to run a python file like an executable. On top of that you are doing it in python interpreter.
Use python's execfile routine to call a file from interpreter or use python test.py to run it from the terminal.
I recognized that the answer I accepted ahead wasn't a perfect method, cause function execfile has been deleted in Python3, the alternative method is using open like:
>>> with open ('/Users/mymac/Documents/Python/test.py', 'r') as f:
... exec(f.read())
...
Still thanks for guys who helped me!
This could be several things. Have you set your environmental correctly? You can test this by cmd: python it should return Python 3.6.4 if that is your current version. If not then head over to tutorialsPoint for how to correctly set up your path.
If that is correctly configured, then have you selected an interpreter in PyCharm. If not, File --> Settings --> Project: Network --> Project Interpreter. Select your python installation path.
Another thing to note is that I suspect you mean to use the terminal instead of the python console.
Then in PyCharms terminal section, python test.py.
I am very new to Python.
I am in command line and I typed the following:
python -m pip install -U pip
Python then starts working inside of command line.
When I type
import pyperclip
pyperclip.copy('testing')
pyperclip.paste()
'Hello World!'
is printed.
When I go to the python shell and type
import pyperclip
pyperclip.copy('testing')
pyperclip.paste()
I get an error message:
ModuleNotFoundError: No module named 'pyperclip'
How can I figure out where command line is looking when I type import pyperclip and where it is looking in the python shell to compare the two?
Edit #1:
import sys
sys.path
Seems to be important. I am guessing that when a module is imported, python checks one of those locations.
pyperclip-1.5.7 is now located in one of the paths specified., the "Lib" directory.
pyperclip-1.5.7 looks like this inside of the folder:
All of the other modules are located as ".py" files just outside of the pyperclip-1.5.7, maybe I need to move some stuff there. I wonder how I can tell python to check inside of this directory...
Edit #2:
I went to this location: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html
On the page, the author writes:
So Python will find any packages that have been installed to those
locations.
Let's see if I can add something to the sys.path location that points to the specific package that is inside the pyperclip-1.5.7 directory and if that helps.
I have both python 3 and python 2 as well. The problem I faced was even after successful pyperclip installation it was not working in Python 3.
The solution I got was to move to the directory of installation of Python 2
C:\Python27\Lib\site-packages
and copied the folders
pyperclip
and
pyperclip-1.7.0-py2.7.egg-info
to the python 3 directory which was in my case
C:\Users\MYNAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
I figured out what was wrong.
I also noticed that there are several posts all over in various locations of people reading the book
Automate the Boring Stuff with Python.
that are also struggling with this part.
After running
import sys
print('\n'.join(sys.path))
I started looking for differences between the output in command line and the output in the python shell. Everything looked extremely similar except in the directory:
lib/site-packages
I copied and pasted what was in this location, as pointed to in command line, to the location pointing to it in the python shell. I closed/reopened the shell and tried running the import pyperclip module, etc again and everything worked like a charm.
Problem is now solved.
I want to try Mako with Django instead of Django's default template language. But I'm having a problem when I try to import Mako's Template class as written in the manual:
from mako.template import Template
mytemplate = Template("hello world!")
print mytemplate.render()
I do this in Windows cmd and receive such an error:
C:\Documents and Settings\User>cd C:\py\project\vendor\template\Mako_73 // cd to where I unpacked Mako
C:\py\project\vendor\template\Mako_73>python // run Python interpreter
>>> from mako.template import Template // trying to import and getting an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".\mako\template.py", line 622
exec code in module.__dict__, module.__dict__
^
SyntaxError: invalid syntax
The code from that part:
def _compile_text(template, text, filename):
identifier = template.module_id
source, lexer = _compile(template, text, filename,
generate_magic_comment=template.disable_unicode)
cid = identifier
if not util.py3k and isinstance(cid, unicode):
cid = cid.encode()
module = types.ModuleType(cid)
code = compile(source, cid, 'exec')
exec code in module.__dict__, module.__dict__
return (source, module)
What can it be? I couldn't find anything in Google about this error.
I'm using Python 3.3.
I've downloaded Mako-0.7.3 as tar.gz file and just unzipped it in
C:\py\poject\vendor\template\Mako_73. I do not have this directory in the PYTHONPATH or paths.pth. C:\py\poject is a directory where my Django project lives and in \vendor\template I've decided to put Mako and import it from there.
UPD
I found the solution. I've installed the Pyramid Framework and have taken the Mako from there as the Mako is a default template system in it. And Pyramid's version works fine.
Your basic problem is that you are using Python 3, which is relatively new for large projects like Django.
Secondly, you need to find out how to install packages correctly. I don't know where you got Mako from, but you won't find anywhere that says "just unpack the tarball" - perhaps you are assuming that from your knowledge of PHP, but it's not correct.
On the Mako site, the suggested method of installation is pip.
If you go for downloading manually, you need to read instructions about installing Python packages, for example here: http://wiki.python.org/moin/CheeseShopTutorial
The reason Mako doesn't work for you is that the installation procedure (which you haven't run) converts all the provided Python 2 code so that it works on Python 3. It is not that someone didn't bother to check the code for basic syntax errors!
If you are trying to use Django, though, Python 3 is definitely the wrong choice - the installation instructions clearly say you need to be using Python 2.5 to 2.7: https://docs.djangoproject.com/en/1.4/intro/install/
Since you are new to Python, you should try to walk before you run, and go with the tried and tested path - which is Python 2.7 for Django. Ignoring installation instructions and requirements will only slow you down and make life hard.