user2158166
user2158166

Reputation: 394

Cannot import module when using memory_profiler

I'm trying to use memory_profiler, but I run into a problem that is isolated in example.py:

#! /usr/bin/env python 
import argparse

def parse_args():
    parser = argparse.ArgumentParser(description='Dummy description')
    parser.add_argument('--option',  action='store_true')
    return parser.parse_args() 

if __name__ == '__main__':
    parse_args()

So basically only using argparse. Standalone this runs fine (I have Python3.3). However, when I issue

$ python -m memory_profiler example.py

I get a the error:

NameError: global name 'argparse' is not defined

Furthermore if I put the line

    parser = argparse.ArgumentParser(description='Dummy description')

Underneath

    if __name__ == '__main__':

And I comment out the function call to parse_args(), then I don't get the error.

Anyone an idea what goes wrong here?

Upvotes: 4

Views: 2523

Answers (1)

ngutzmann
ngutzmann

Reputation: 51

In my experience the memory-profiler does not use the current directory in its PYTHONPATH so my fix has been to use the following command:

PYTHONPATH=./<CORRECT_PATH> python -m memory_profiler example.py

Upvotes: 1

Related Questions