简体   繁体   中英

How to import a module in Python?

I've written a class in its own .py file and I want to be able to use it in other files. I saved the file panel.py in C:\\Python27\\Lib and then I tried to use import panel in ipython and I got the following error ImportError: No module named panel . I thought I had possibly saved the class in the wrong directory so I tried saving the file here C:\\Python27 , which also did not work when I tried to import the file. What am I doing wrong? If its useful here's the class I wrote, I deleted everything that the class does which should not impact anything.

import statsmodels.formula.api as sm
import pandas as pd
import numpy as np
import scipy as sp
from math import sqrt
import re


class PanelModel:


    def __init__(self, formula, effects = "random", time_fe = False, entity_fe = False, robust = False, data = None):
        pass

    def balanceChecker(self):
        pass

    def fit(self):
        pass

    def randomEffects(self):
        pass

    def fixedEffects(self):
        pass

    def betasREBalanced(self):
        pass

    def summary(self):
        pass

I'm running python 2.7.9 and ipython 3.0.0

from https://docs.python.org/2/tutorial/modules.html

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • List the directory containing the input script (or the current directory). PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  • List the directory containing the input script (or the current directory). PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  • List the installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

I'm guessing what you're looking for is dynamic importing.

Try this. See if it works

importlib.import_module(moduleName)

The most simple solution is to make sure you are in the same directory as the module you wrote. Type ls from the ipython prompt and see if panel.py is listed.

Other possible solutions are moving your module somewhere listed in sys.path or appending the path to your module to sys.path

Thank you everyone for your prompt and very helpful suggestions. I opted to import sys and then print sys.path and save my file in one of those directories, which required unhiding my AppData folder.

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