0

I am new to python and I am running into some troubles importing modules. I have very limited prior knowledge to programming (bit of Fortran and C) and I usually work with Matlab.

I searched the web for import methods and the preferred method according to most tutorials is: import X

If I use the console (iPython respectively) I can execute the following:

import scipy
scipy.stats.cauchy.pdf

However when I use the same line in a function it does not work, I have to use

import scipy.stats as s
s.stats.cauchy.pdf

or I get: AttributeError: 'module' object has no attribute 'stats'

By reading this 'import module' or 'from module import' I got the impression that both should work.

If someone could quickly clear that up for me I would be very thankful!

Best Wishes, Chris

wim
  • 338,267
  • 99
  • 616
  • 750
Chris
  • 89
  • 2
  • 8

1 Answers1

1

It should not happen on a newly opened ipython instance. You would not normally be able to access like this:

import scipy 
scipy.stats.cauchy.pdf

Without getting AttributeError, in either ipython or regular python interpreter. You would need to do

import scipy.stats

somewhere, because it is a submodule.

Either you have an earlier imported scipy.stats hanging around in your ipython namespace, or you have some automatic startup script importing it when ipython starts.

Note: if you are using ipython's run magic function to execute a script, any global variables and imported modules will remain in scope.

wim
  • 338,267
  • 99
  • 616
  • 750