Python Standard Library not installed? - python

I'm fairly new to Python and recently started development on a new mac mini. As you know it comes with 2.7 installed.
The problem I'm running across is the Standard Library doesn't seem to be installed.
I get a syntax error when importing modules (SyntaxError: invalid syntax).
I was running this code with the argv datafile.csv
import csv
import sys
stocks.csv = argv
f = open(sys.argv[1], 'rt')
try:
reader = csv.reader(f)
for row in reader:
print row
finally:
f.close()
After awhile of this I decided to run the interpreter and get help.
When I ran the interpreter and do help(csv) or most other modules (sys works just fine) I get this error:
>>> import csv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "csv.py", line 1
import
^
SyntaxError: invalid syntax
Do I need to install the library or am I accessing it wrong?
Thanks.

Do you have a file in the current directory named csv.py with a blank import statement on line 1? Python searches the current directory for imports before it looks at system directories.

importing module that can not be found causing
ImportError: No module named {...}
error SyntaxError means that text in file "csv.py" isn't valid python code.
Maybe you have your own "csv.py" somewhere that is shadowing original csv lib?

Related

Error: No module named 'pymem.process'; 'pymem' is not a package

I am trying to learn how to use pymem in Python.
I have tried to make two different programs according to two tutorials I have seen but I always get the same error when I try to run the code.
I have this:
from pymem import Pymem
pm = pymem("ac_client.exe")
health = pm.read_int(0x007B43F4)
print ("Health: ", health)
But when I try to run the code I get the following error:
Traceback (most recent call last):
File "c:\Users\N\Desktop\PYTHON\pymem\pymem2.py", line 1, in <module>
from pymem import Pymem
File "c:\Users\N\Desktop\PYTHON\pymem\pymem.py", line 4, in <module>
from pymem.process import *
ModuleNotFoundError: No module named 'pymem.process'; 'pymem' is not a package
I have pymem installed in it's latest version from Visual Studio Code. And in the videos I've seen (one is from a few months ago) they have the same code as me.
You have a file named pymem.py that's mentioned in the exception message (specifically c:\Users\N\Desktop\PYTHON\pymem\pymem.py). This locally written pymem module is shadowing the pymem package you've installed elsewhere (python\python310\lib\site-packages according to one of your comments, though that path is not complete).
You need to rename your pymem.py file to something else if you want to be able to use the package.
I think you don't have it installed, try going into cmd, Terminal or whatever and type pip install pymem.
pip should install the package for you and you should be good.

Trying to import a module containing old print statements into new python 3.6

I am trying to import the paraview.simple module from Paraview 5.2 into the new Python 3.6.2 using IDLE. Unfortunately it appears this module is using the old print statement as suggested by this error message:
>>> import paraview.simple
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
import paraview.simple
File "C:\Program Files\ParaView 5.0.1\lib\paraview-5.0\site-packages\paraview\__init__.py", line 129
print text
^
SyntaxError: Missing parentheses in call to 'print'
I really want to use this version of python? There are other modules I wish to use alongside paraview.simple which are only compatible with the latest version of python. How can I circumvent this minor print issue and import the modules I need?
It seems like the question might not be relevant anymore since paraview hasn't been supported for long time (at least there haven't been any pull requests since 2017 https://github.com/Kitware/ParaView/pulls).
For those who still want to use it there is a solution as pointed by #Shashank in the comments: just convert it from python2 to python3 using 2to3 or similar tool.

Sha-3 in python implementation

I am trying to implement sha-3 in python.The code given below is how I implemented it.But i am getting the below error again and again.
import sys
import hashlib
arg1 = sys.argv[1]
with open(arg1, 'r') as myfile:
data=myfile.read().replace('\n', '')
import sha3
s=hashlib.sha3_228(data.encode('utf-8')).hexdigest()
print(s)
The following error is what i get when I execute it.
Traceback (most recent call last):
File "sha3.py", line 6, in <module>
import sha3
File "/home/hello/Documents/SHA-3/sha3.py", line 7, in <module>
s=hashlib.sha3_228(data.encode('utf-8')).hexdigest()
AttributeError: 'module' object has no attribute 'sha3_228'
The below link can be used for reference.
https://pypi.python.org/pypi/pysha3
There are two problems here: one from your code, and one from the documentation, that contains a typo on the function you would like to use.
You are calling a function that is not present in hashlib library. You want to call function sha3_228 from module sha3, that is shipped with package pysha3. In fact, sha3_228 does not exist, it is sha3_224 that exists.
Simply replace hashlib.sha3_228 with sha3.sha3_224.
And make sure you have installed pysha3, with command
python -m pip install pysha3
Here is an example
import sha3
data='maydata'
s=sha3.sha3_224(data.encode('utf-8')).hexdigest()
print(s)
# 20faf4bf0bbb9ca9b3a47282afe713ba53c9e243bc8bdf1d670671cb
I had the same problem. I installed sha3 by itself first. That doesn't work. Then I installed pysha3 and it still didn't work. I finally uninstalled both sha3 and pysha3. Then I reinstalled just pysha3 and it worked fine.
You likely need to include:
import sys
if sys.version_info < (3, 6):
import sha3
This is because is lower versions of python sha3 aren't included by default in hashlib.

Unable to debug interactively with Python 2.7

I had asked a question previously on relative paths in python in SO question: How can I access relative paths in Python 2.7 when imported by different modules
The provided answer worked great in all of my scripts and functions. However, when trying to debug the files in IDLE (Python 2.7) it generates run time errors.
Can anyone point me to documentation on using the __file__ notation? Also I would like to understand why IDLE generates errors while running the sample code but running the same file from the command line or double clicking it (for the windows users) does not.
Any help would be greatly appreciated!
Note that I am running Python 2.7 on Windows XP with virtualenv (unactivated during these tests).
Sample Code
import os
import sys
curdir = os.path.dirname(__file__)
sys.path.append(curdir + '/..')
Error
Traceback (most recent call last):
File "C:\MyFile.py", line 3, in `<module>`
curdir = os.path.dirname(`__file__`)
NameError: name '`__file__`' is not defined
__file__ won't be set if you're writing this in the interpretor.
So:
>>> import os, sys
>>> curdir = os.path.dirname(__file__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
Is expected.
__file__ is the name of the file that was called by the python interpretor - so if you ran this from a script it would work.
$ python curdir.py
$
(The script is exactly the same as what I put into the interpretor, hence no error or output)
From what I've observed using IDLE before, it acts as an interpretor - so it'll run the file in question. However, it wasn't started with that file, so the __file__ is never set.

Why am I getting the following error in Python "ImportError: No module named py"?

I'm a Python newbie, so bear with me :)
I created a file called test.py with the contents as follows:
test.py
import sys
print sys.platform
print 2 ** 100
I then ran import test.py file in the interpreter to follow an example in my book.
When I do this, I get the output with the import error on the end.
win32
1267650600228229401496703205376
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py
Why do I get this error and how do I fix it? Thanks!
Instead of:
import test.py
simply write:
import test
This assumes test.py is in the same directory as the file that imports it.
This strange-looking error is a result of how Python imports modules.
Python sees:
import test.py
Python thinks (simplified a bit):
import module test.
search for a test.py in the module search paths
execute test.py (where you get your output)
import 'test' as name into current namespace
import test.py
search for file test/py.py
throw ImportError (no module named 'py') found.
Because python allows dotted module names, it just thinks you have a submodule named py within the test module, and tried to find that. It has no idea you're attempting to import a file.
You don't specify the extension when importing. Just do:
import test
As others have mentioned, you don't need to put the file extension in your import statement. Recommended reading is the Modules section of the Python Tutorial.
For a little more background into the error, the interpreter thinks you're trying to import a module named py from inside the test package, since the dot indicates encapsulation. Because no such module exists (and test isn't even a package!), it raises that error.
As indicated in the more in-depth documentation on the import statement it still executes all the statements in the test module before attempting to import the py module, which is why you get the values printed out.

Categories