'NameError: name 'argparse' is not defined

I am using python 3.7.4, here is my code but it doesn't work. It's a ssrf scanner wrote by python, I just started to learning 'argparse' so don't know what wrong in the code:

import sys
import argparse

error = "Please enter a valid command. If you don't know how to use, enter '--help'"
parser = argparse.ArgumentParser(version='1.0')
parser.add_argument('-u', help='URL', dest='url')
parser.add_argument('--get', help='Method GET', dest='get')
parser.add_argument('--post', help='Method POST', dest='post')
parser.add_argument('-i', help='Ip', dest='ip')
parser.add_argument('-p', help='Port', dest='port')
parser.add_argument('-d', help='Post data', dest='param')
parser.add_argument('--encode', help='Encode payloads', dest='encode')
parser.add_argument('--proxy', help='Use proxy(y|ies)',
                    dest='proxy', action='store_true')
parser.add_argument('-f', help='Load payloads from a file', dest='file')
parser.add_argument('-h', help='Add headers',
                    dest='header', const=True)
parser.add_argument('--white', help='Whitelist', dest='wlist')
parser.add_argument('--black', help='Blacklist', dest='blist')

url = args.url
get = args.get
post = args.post
ip = args.ip
port = args.port
param = args.param
encode = args.encode
proxy = args.proxy
file = args.file
header = args.header
wlist = args.wlist
blist = args.blist

if not url:
    print(error)
    quit()

if not "http://" in url or "https://" in url:
    print("Please enter full URL(Include 'https://' or 'http://)")
    quit()

if not get and post:
    print(error)
    quit()

if post:
    if not param:
        print(error)
        quit()

from mode import scan        
from mode import test

Even I have import 'argparse', an error still appear:

Traceback (most recent call last):
  File "E:\SSdom\ssrf.py", line 7, in <module>
    parser = argparse.ArgumentParser(version='1.0')
NameError: name 'argparse' is not defined

Can anyone help me, please! (Sorry if this is a stupid question)



Solution 1:[1]

I had a similar issue and it helped to use from argparse import ArgumentParserdirectly and then use ArgumentParser() as an imported function. Hopefully that works for you.

Solution 2:[2]

Removing whatever is inside the parenthesis solved this issue for me.

This error popped up for me when I used parser = argparse.ArgumentParser(description = usage) and resolved by doing the following parser = argparse.ArgumentParser()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jdjame
Solution 2 Rachel