Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in a python2 file
#1
Hi all ! Because I installed "click" being in python2, I can import "click" only on python2. I cannot use 3.6 for this file. My problem appears at line 19.
import click
import math
print("solver.py  in python2.7")

class Solver:
    def demo(self):
        print("Resolve second degree equation. Enter the coefficients(numbers): a,b,c")
        text = raw_input(' Are you ready: y/n ? ')
        print('you entered:  ', text)
        text = "y"
        while (text == 'y'):
            # while True:
            a = float(input("a "))
            b = float(input("b "))
            c = float(input("c "))
            d = b ** 2 - 4 * a * c
            if d >= 0:
                disc = math.sqrt(d)
                root1 = (-b + disc) / (2 * a)
                root2 = (-b - disc) / (2 * a)
                print(root1, root2)
            else:
                print('This equation has no roots')

            print('do you want to begin again for another equation ?  ')
            #text = raw_input(' y/n  ?   ')
            #print("you entered: ", text)
			click.echo('Do you agree ? y/n ', nl=False)
			text= click.getchar()
			click.echo() 
			if text == b'y':
				click.echo('We wiil continue')
			elif text == b'n':
				click.echo('Abort!')
				exit()
			else:
				click.echo('Invalid input ! ')
				exit()
			

Solver().demo()
                

	
Reply
#2
click is available for python 3
make sure your pip is for python 3:
pip -V
you should see something to similar to:
Output:
(venv) Larz60p@linux-nnem: BusinessLists:$pip -V pip 18.0 from .../BusinessLists/venv/lib/python3.7/site-packages/pip (python 3.7) (venv) Larz60p@linux-nnem: BusinessLists:$
important thing is that version is python 3+
if not use pip3
pip install click
# or if necessary
pip3 install click
Reply
#3
Python 3 comes with pip since 3.4.
python3 -m pip install click will also work. Just to be sure that python3 pip will be used.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
import click
import math
print("solver.py in python2.7")

class Solver:
def demo(self):
print("Resolve second degree equation. Enter the coefficients(numbers): a,b,c")
text = raw_input(' Are you ready: y/n ? ')
print('you entered: ', text)
text = "y"
while (text == 'y'):
# while True:
a = float(input("a "))
b = float(input("b "))
c = float(input("c "))
d = b ** 2 - 4 * a * c
if d >= 0:
disc = math.sqrt(d)
root1 = (-b + disc) / (2 * a)
root2 = (-b - disc) / (2 * a)
print(root1, root2)
else:
print('This equation has no roots')

print('do you want to begin again for another equation ? ')
#text = raw_input(' y/n ? ')
#print("you entered: ", text)
click.echo('Do you agree ? y/n ', nl=False)
text= click.getchar()
click.echo()
if text == b'y':
click.echo('We wiil continue')
elif text == b'n':
click.echo('Abort!')
exit()
else:
click.echo('Invalid input ! ')
exit()



sylvain@sylvain-HP-Laptop-15-bw0xx:~$ python solver.py
File "solver.py", line 29
click.echo'Do you agree ? y/n '
^
IndentationError: unexpected indent
sylvain@sylvain-HP-Laptop-15-bw0xx:~$ pip -V
pip 18.0 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
I am on Linux. All my problems should disappear if I could easily make python3 the default language.
Reply
#5
(Aug-07-2018, 10:08 AM)sylas Wrote: All my problems should disappear if I could easily make python3 the default language.
This is not true, making python 3 the default language would not solve any problem. You can use python 3 and the click module without python 3 being the OS' default python. Trying to change the default python is a very bad idea. Don't do that.
Reply
#6
@wavic
sylvain@sylvain-HP-Laptop-15-bw0xx:~$ python3 -m pip install click
Requirement already satisfied: click in /usr/lib/python3/dist-packages (0.4.46+17.10.20170607.3.0ubuntu1)
launchpadlib 1.10.5 requires testresources, which is not installed.
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
@Larz click works well on python 2.7
#python2
import click

click.echo('Do you agree ? [y/n] ', nl=False)
c = click.getchar()
click.echo()
if c == b'y':
    click.echo('We will go on')
elif c == b'n':
    click.echo('Abort!')
    exit()
else:
    click.echo('Invalid input ! ')
    exit()
    
    
print("sylas")
Reply
#7
What is the output of python3 -c "import click; print(click)"?
Reply
#8
sylvain@sylvain-HP-Laptop-15-bw0xx:~$ python3 -c "import click; print(click)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'click'
Reply
#9
(Aug-07-2018, 10:08 AM)sylas Wrote: All my problems should disappear if I could easily make python3 the default language.
You use python3 and pip3 with default setup that Ubuntu has.
I did show you pipenv in an other post.
This has been up several with you and as usual you struggle with all you do Undecided

This mean that you use python3 my_script.py to run code with Python 3.
pip3 install some_package to install to Python 3.
Linux Python 3 environment

Should use decorator with Click eg @click.command().
Basic Concepts.
Reply
#10
Please forget all things about pips and different python languages. Consider that the little file concerning pick, works very well on python2. Same thing for the solver equation file. The problem arrises when I try to join these 2 files. The interpreter tells that there is an indentation error, but I am sure he tells false.
Can someone help to finish this job ? Thanks in advance.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python3 decoding problem but python2 OK mesbah 0 1,770 Nov-30-2019, 04:42 PM
Last Post: mesbah
  Trying to run a python2 script dagamer1991 3 2,485 Aug-12-2019, 12:33 PM
Last Post: buran
  python2.7 executables thus the system python2.7 was erroring utility.execute()? vivekm 1 1,727 May-20-2019, 11:24 AM
Last Post: vivekm
  Python2 is not supported Skaperen 2 2,127 Mar-01-2019, 07:50 PM
Last Post: stranac
  Posting zip file on HTTP network using Python2.7 deepti92 1 6,749 May-15-2018, 12:41 PM
Last Post: deepti92

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020