James A. Foster
James A. Foster

Reputation: 151

How to import my own Python packages

I have the following directory structure:

~/python_libs
    /markets
        Players.py
        Commodity.py
        Markets.py
        Currency.py
        __init__.py

and my __init__.py is this:

import numpy.random as rn
import matplotlib.pyplot as plt
from time import gmtime, strftime
from datetime import datetime

I have set $PYTHONPATH=${HOME}/python_lib and verified that this has become part of sys.path (from import sys)

Now, I go into ipython and import markets

Nothing. It gives a blank line, but if I use, say p=Player() I get

name 'Player' is not defined

I'm stumped. How do I import markets and have access to the modules in the markets package?

Upvotes: 2

Views: 61

Answers (1)

rczajka
rczajka

Reputation: 1840

If Player is defined in markets.Players, you'll need:

from markets.Players import Player
p = Player()

or:

import markets.Players
p = markets.Players.Player()

Either way, in Python you need to explicitly import what you need into your namespace.

Upvotes: 4

Related Questions