简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM