I have a simple sample script written in python2. Since we are migrating to python3, I am trying to get familiar with the tools that ae present. The modernize tool, helps me to achieve a python3 code. I run it, I get the result expected.
However, modernize promises backward compatibility, I expect the newly generated code to run in python2 as well.
While running with python2 Interpreter, I face issues as shown below:
Eg:
Sample.py
import Queue
from urllib2 import urlopen
def greet(name):
print 'Hello',
print name
print "What's your name?",
name = raw_input()
greet(name)
From the directory on the commandline
python-modernize -w Sample.py
New Sample.py:
from __future__ import absolute_import
from __future__ import print_function
import six.moves.queue
from six.moves.urllib.request import urlopen
from six.moves import input
def greet(name):
print('Hello', end=' ')
print(name)
print("What's your name?", end=' ')
name = input()
greet(name)
I run the new script from the command line. Produces correct results.
py Sample.py
However, since it is backward compatible, when I do the following I get errors:
C:\Python27\python.exe Sample.py
Traceback (most recent call last):
File "Sample.py", line 3, in <module>
import six.moves.queue
ImportError: No module named six.moves.queue
Should the new script be modified again? Is modernize not fully backward compatible?
Please let me know
Did you install the six module ?
pip install six
https://pypi.org/project/six/
The answer for this is on the same regard as pip installing libraries in different versions. I was installing the library in python3. So now I managed to get it done on python2. Refer here
PythonDocumentation
Related
import pyinputplus as pyip
while True:
prompt='Want to know how to keep an idiot busy for hours?\n'
response=pyip.inputYesNo(prompt)
if response=='no':
break
print('Thank you. Have a nice day.')
When I run my above code , I get this error:
Traceback (most recent call last):
File "c:\users\XXXXXX\mu_code\idiot.py", line 1, in <module>
import pyinputplus as pyip
File "c:\users\XXXXXX\mu_code\pyinputplus\__init__.py", line 15, in <module>
import pysimplevalidate as pysv # type: ignore
ModuleNotFoundError: No module named 'pysimplevalidate'
I cannot figure it out. The module is definitely installed. I've even moved it from the folder it was originally installed in to the mu folder where the py file is saved. Any help would be appreciated.
The ModuleError says that you do not have pysimplevalidate installed.
Using the same python executable as you are using to run your script (idiot.py), run
python -m pip install pysimplevalidate
or, even more bullet-proof:
<path_to_python.exe> -m pip install pysimplevalidate
If you are not sure what python executable the script is using, you can check it with
# put this on top of your script
import sys
print(sys.executable) # will print C:\path\to\python.exe
I am using Raspberry PI 3 Model B running Kali Linux and i am currently coding a P2P encrypted python chat that runs on python 3. The cryptographic library i am using is called "CryptoShop", which is a '.py' file, not a imported library. I use it the same way that it's 'README' file instructed, so this is not a thing. Before i adeed crypto to the chat, it worked well, but now, i'm having errors since CryptoSHop uses the TQDM math library, and tryed installing it using APT-GET, PIP, by Source and nothing, because, firstly my chat only runs on Python3:
root#kali:~# python PyChat/pychat.py
File "PyChat/pychat.py", line 16
SyntaxError: Non-ASCII character '\xc3' in file PyChat/pychat.py on line 16, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
So when i use Python3:
root#kali:~# python3 PyChat/pychat.py
Traceback (most recent call last):
File "PyChat/pychat.py", line 4, in <module>
from cryptoshop import encryptstring
File "/root/PyChat/cryptoshop.py", line 52, in <module>
from tqdm import *
ModuleNotFoundError: No module named 'tqdm'
CryptoSHop try importing tqdm.
Here's a piece of it's code:
import os
import sys
from tqdm import *
import getpass
import argparse
I am still on the level of basic coding, i get this piece of chat code on the web, and just adeed to it basic user authentication (check if file whit the username exists), improved usability, and adeed crypto.
And sorry by my bad english, it's not my native language ;-)
Thanks in advance.
I am spanish, this happend when you use acents or special charts.
Add this in your first line:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
And use pip3 for install tqdm in python3
pip3 install tqdm
Im trying to import a module called geoip2 from pypi into python it is not included in its standard libraries.
I open command prompt and type:
pip install geoip2
The command prompt returns
Successfully installed geoip2-2.4.2
After it is installed I try importing it using IDLE:
import geoip2.webservice
which returns the error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import geoip2.webservice
ImportError: No module named 'geoip2'
Although it is installed already I cannot use it. How can i prevent this? Take note that I use python 3.6
May be you have two different version of Python installed. Try opening IDLE using the Python version where you have installed geoip.
Instead of:
import geoip2.webservice
Try doing:
import geoip2
from geoip2 import webservice
Since geoip2.webservice is not installed, geopip2 is and .webservice is an function object of that module.
Further, you can avoid typing geoip2.webservice every time by doing:
import geoip2
from geoip2 import webservice as gws
Then anytime you want to run the .webservice function, you can just use gws.
Alternatively:
Just do:
import geoip2
Then in your script you can call it:
geoip2.webservice(#do stuff here or however you call the function)
I have a set of three programs that I am trying to combine into one. They all work individually, but I am having issues when trying to get them to work together. The issue I am currently having is with the first section of code:
import os
import sys
from contextlib import closing
import colorama # $ pip install colorama
import docopt # $ pip install docopt
import socks # $ pip install PySocks
import stem.process # $ pip install stem
from sockshandler import SocksiPyHandler # see pysocks repository
from stem.util import term
try:
import urllib2
except ImportError: # Python 3
import urllib.request as urllib2
args = docopt.docopt(__doc__, version='0.2')
colorama.init(strip=not (sys.stdout.isatty() or args['--color']))
When I run the program, I get this error:
Traceback (most recent call last):
File "cilantro.py", line 34, in <module>
args = docopt.docopt(__doc__, version='0.2')
File "C:\Python34\lib\site-packages\docopt.py", line 558, in docopt
DocoptExit.usage = printable_usage(doc)
File "C:\Python34\lib\site-packages\docopt.py", line 468, in printable_usage
raise DocoptLanguageError('"usage:" (case-insensitive) not found.')
docopt.DocoptLanguageError: "usage:" (case-insensitive) not found.
Why am I getting this error message? The same code works fine in the original program.
When using docopt you need to write a __doc__ string for your script. docopt parses this string to work out how to handle the command line options and arguments.
To fix this, and something like the following text above your "import os" (so it's the first thing in the file):
"""
Name.
Describe what this script does
Usage:
name <firstarg>
name --countdown
name sillycommand <SILLYNESS>
name -h | --help
Options:
--countdown display a count down
"""
More details on what to put in this usage string can be found at the docopt documentation, http://docopt.org/
I am completely new to Python and wanted to use py2neo and tornado module.
In order to do that I ran setup.py for both modules and placed them into folders
C:\Python32\modules\py2neo
and
C:\Python32\modules\tornado
In the main program I guess these lines tell the interpreter where to look for files:
import sys
sys.path.append(r'C:\Python32\modules')
# Import Neo4j modules
from py2neo import neo4j, cypher
Reading the book I also added environmental variable (in Windows 7)
PYTHONPATH = C:\Python32\modules;C:\Python32\modules\tornado;C:\Python32\modules\py2neo
Edit
Now I figured out that Python Shell has to be restarted in order to load modified PYTHONPATH variable
In case the variable value is PYTHONPATH = C:\Python32\modules
and the program contains the line
from py2neo import neo4j, cypher
then the following lines are useless:
import sys
sys.path.append(r'C:\Python32\modules')
When I run the program however I get the following error:
Traceback (most recent call last):
File "C:\...\Python Projects\HelloPython\HelloPython\Hellopy2neo.py", line 15, in <module>
from py2neo import neo4j, cypher
File "C:\Python32\modules\py2neo\neo4j.py", line 38, in <module>
import rest, batch, cypher
ImportError: No module named rest
In the file neo4j.py there are the following lines:
try:
import json
except ImportError:
import simplejson as json
try:
from urllib.parse import quote
except ImportError:
from urllib import quote
try:
from . import rest, batch, cypher
except ImportError:
import rest, batch, cypher #line38
and rest.py file is located in folder C:\Python32\modules\py2neo so I don't know why I get the error
ImportError: No module named rest
Edit2:
Trying to import the py2neo directoy in Python Shell and list modules I get:
>>> import py2neo
>>> [name for name in dir(py2neo) if name[0] != '_']
['rest']
I guess there are some unneccesary imports as well and would be very thankful if anyone explained, which imports should be added and excluded (in PYTHONPATH and scripts) in order the program to run without errors.
I suspect the problem is that the import syntax for relative imports has changed in transition from Python 2 to Python 3:
The only acceptable syntax for relative imports is from .[module]
import name. All import forms not starting with . are interpreted as
absolute imports.
The modules you installed use the syntax that would work in Python 2. You could either install them for Python 2, or look for a version of py2neo that supports Python 3, or try to port it manually (the import line should look like from . import rest, but you'll probably face other problems later) or with 2to3 tool.
Update: I tried installing py2neo with pip. It failed for Python3 and finished successfully for Python 2. The version is 1.2.14.