urllib.request not found in .py code - windows

I'm working with Python 3.4 on Windows:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
At the Python command line, everything works fine (no errors):
>>> import urllib.request
>>>
Then I write the following single-line code test.py:
import urllib.request
If I execute test.py I get:
Traceback (most recent call last):
File "Z:\user\test.py", line 1, in <module>
import urllib.request
ImportError: No module named request
I tried various things (obviously unsuccessfully!), so I'm absolutely baffled. Can anyone give me a clue??...

Make sure that you run the script using Python 3 interpreter e.g., configure your system to use Python launcher py and add the appropriate shebang (#!/usr/bin/env python3) in your Python script.

Related

not able to import python module despite setting PYTHONPATH

I am trying to concatenate some pdf files from the command line on mac.
After reading online I found there is a python script for this located here:
/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py
After resetting my python to be the system's 2.7 python (sudo port select --set python python27), I get this error when trying to run the above script:
Traceback (most recent call last):
File "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py", line 23, in <module>
from CoreFoundation import *
ImportError: No module named CoreFoundation
So apparently python is not able to import something called CoreFoundation. Looking, I find said CoreFoundation.py at this place on my file system:
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/Carbon/CoreFoundation.py
And, indeed, inspecting the sys.path variable seems to indicate that the above directory is somehow overlooked:
$ python
>>> import.sys
>>> sys.path
['', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages']
(Pardon the long unbroken line. The point is, things like
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages'
are found in sys.path, but the arefore-mentioned
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/Carbon'
is not.) So I try to manually add to PYTHONPATH (which was previously empty):
PYTHONPATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/Carbon"
...but even after doing this, I get the same ImportError: No module named CoreFoundation error.
What should I do?

Unable to imoprt modules on python script while running on cmd

I can not run python scripts on Command Prompt which has to import any module whereas I can run that script without any error on python IDLE. I think, this is happening after I install Anaconda on my machine.
Even if I run "Python" command it starts the shell and then there I can import any module that I want without any error.
C:\Users\USER>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt4
>>>
But when I try to run a script which has to import any module directly It rises a "ModuleNotFoundError" error.
E:\Python\PyQt4\Apps\test>main.py
Traceback (most recent call last):
File "E:\Python\PyQt4\Apps\test\main.py", line 9, in <module>
from PyQt4 import QtCore, QtGui
ModuleNotFoundError: No module named 'PyQt4'
E:\Python\PyQt4\Apps\test>
I don't know why it is happening

Missing required dependencies when running from CMD

My problem is as follows. I'm trying to package a python script a wrote to run on other computers with CX_freeze. After some fiddling I got the script packaged but when I went to run the code I got:
Miless-foobar-Pro:~ milesconn$ /Users/milesconn/\~/Documents/ IPgeolookupforXLSX ; exit;
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- packages/cx_Freeze/initscripts/__startup__.py", line 14, in run
module.run()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- packages/cx_Freeze/initscripts/Console.py", line 26, in run
exec(code, m.__dict__)
File "/Users/milesconn/Desktop/IPgeolookupforXLSX.py", line 1, in <module>
import sys
ModuleNotFoundError: No module named 'pandas'
logout
[Process completed]
Now this was quite confusing because if I ran the script from IDLE everything worked perfectly. I started trying to run the original script (the .py not the .app) from terminal with simply python IP* but I continued to get the ModuleNotFoundError I upgraded Pandas w/ Pip3 and everything is up to date, still no luck. The program however worked perfectly when run in IDLE. I went on searching for my problem and found this link. I followed the advice and added to the top of my program
import sys
sys.path.insert(1,'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages')
import pandas as pd
import numpy as np
Still the program ran from IDLE perfectly like before and now when I ran it in Terminal got this error
Traceback (most recent call last):
File "IPgeolookupforXLSX.py", line 4, in <module>
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/__init__.py", line 19, in <module>
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
Now I don't know what to do from here. I believe if I get the program to run from terminal correctly then it'll hopefully couple with CX_Freeze and run. Thanks.
tl;dr: .py script runs from IDLE perfectly but when run from terminal it returns a ImportError: Missing required dependencies ['numpy']

Python won't run due to ImportError: cannot import MAXREPEAT

I am new to python but have been using both IDLE and EricIDE for a few weeks without any major problems.
I was editing a program I had written that called random.randint() function and it wouldn't work.
Previously, this program had been working and I had not changed that call out.
I then loaded another program that uses the same function that had been working and it would not run either.
I tried to load the program in IDLE but IDLE wouldn't load. After trying several reboots and reloads EricIDE wouldn't load either. I noticed a black window popping up and disappearing quickly when I try to launch either IDE from my previously working desktop shortcuts.
Searching for help led me to run python shell from the windows command line by going to C:\python33\ and typing "python" to run python shell, I get:
File "C:\python33\lib\sre_constants.py", line 18, in (module)
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
I am using Windows 8 (new to it as well and still trying to figure it out).
At this point I'm assuming my problem is with my python installation since the python shell won't work. I have uninstalled and reinstalled Python 3.3.1 but the problem persists. I also deleted the .idlerc folder from my Users directory as suggested in another thread that was similar to my problem but that doesn't seem to have helped either.
Thank you for any help you can provide.
response to eryksun:
C:\Python33>python.exe -c "import sys; print(sys.path)"
Traceback (most recent call last):
File "C:\Python33\lib\site.py", line 70, in <module>
import re
File "C:\Python33\lib\re.py", line 122, in <module>
import sre_compile
File "C:\Python33\lib\sre_compile.py", line 14, in <module>
import sre_parse
File "C:\Python33\lib\sre_parse.py", line 17, in <module>
from sre_constants import *
File "C:\Python33\lib\sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
C:\Python33>python.exe -S -c "import sys; print(sys.path)"
['', 'C:\\Python33\\python33.zip', 'C:\\Python33\\DLLs',
'C:\\Python33\\lib', 'C:\\Python33']
Follow up to to eryksun:
C:\Python33>python.exe -S -c "import _imp; _sre = _imp.init_builtin('_sre');
print(_sre.MAXREPEAT)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MAXREPEAT'
I suggest you uninstall. Completely remove C:\Python33 and also C:\Windows\System32\python33.dll. _sre is built in to the latter DLL. MAXREPEAT is set by its initialization functionPyInit__sre (Modules/_sre.c). Clearly, something is wrong there.
When you download the 3.3.1 installer, make sure you get the right binary for your platform, i.e. x86 for 32-bit Windows and X86-64 for 64-bit Windows.

Unable to import argparse 1.2.1 for python 2.5 on windows

So, I thought that I had correctly installed the setuptools and argparse 1.2.1.
C:\Python25_64bit>python ez_setup.py argparse
Searching for argparse
Best match: argparse 1.2.1
Processing argparse-1.2.1-py2.5.egg
argparse 1.2.1 is already the active version in easy-install.pth
Using c:\python25_64bit\lib\site-packages\argparse-1.2.1-py2.5.egg
Processing dependencies for argparse
Finished processing dependencies for argparse
C:\Python25_64bit>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:19:34) [MSC v.1400 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named argparse
>>> quit()
However, it seems that I can't import it, as you can see above. I also tried directly adding the egg to my Eclipse PyDev projects, but this was to no avail, either. As you can see, nothing is set in my environment. Perhaps, this is the problem?
C:\Python25_64bit>echo %PYTHONPATH%
%PYTHONPATH%
C:\Python25_64bit>echo %PYTHONHOME%
%PYTHONHOME%
I hope you paid attention while installing and noted where argparse was installed.
In your script (run from the commandline or from your IDE) include at the top:
import sys
for p in sys.path:
print repr(p)
to see all the paths python is searching through. Either add the path to argparse using:
sys.path.insert(0, '/your/path/to/argparse')
or move the install to something in the path.
The environment variables can be empty, there are compiled in elements for sys.path as well.

Resources