无法在python包中导入文件

时间:2019-03-25 10:26:19

标签: python package python-import

我正在尝试创建python软件包,但导入时遇到问题。这是当前情况:

[lpuggini@machinelearn-1 src]$ source ~/VirtualEnvs/time_series_algorithms_venv/bin/activate
(time_series_algorithms_venv) [lpuggini@machinelearn-1 src]$ tree                   
.
|-- TimeSeriesAlgorithms.egg-info
|   |-- PKG-INFO
|   |-- SOURCES.txt
|   |-- dependency_links.txt
|   `-- top_level.txt
|-- requirements.txt
|-- setup.py
`-- time_series_algorithms
    |-- __init__.py
    |-- __init__.pyc
    |-- examples
    |   |-- example_percentage_variation.py
    |   `-- example_percentage_variation.py~
    |-- percentage_varation.py
    `-- percentage_varation.pyc

3 directories, 12 files
(time_series_algorithms_venv) [lpuggini@machinelearn-1 src]$ python setup.py develop
/home/lpuggini/VirtualEnvs/time_series_algorithms_venv/lib/python2.7/site-packages/setuptools/dist.py:475: UserWarning: Normalizing '0.1dev' to '0.1.dev0'
  normalized_version,
running develop
running egg_info
writing TimeSeriesAlgorithms.egg-info/PKG-INFO
writing top-level names to TimeSeriesAlgorithms.egg-info/top_level.txt
writing dependency_links to TimeSeriesAlgorithms.egg-info/dependency_links.txt
reading manifest file 'TimeSeriesAlgorithms.egg-info/SOURCES.txt'
writing manifest file 'TimeSeriesAlgorithms.egg-info/SOURCES.txt'
running build_ext
Creating /home/lpuggini/VirtualEnvs/time_series_algorithms_venv/lib/python2.7/site-packages/TimeSeriesAlgorithms.egg-link (link to .)
TimeSeriesAlgorithms 0.1.dev0 is already the active version in easy-install.pth

Installed /home/lpuggini/mlp/time_series_algorithms/trunk/src
Processing dependencies for TimeSeriesAlgorithms==0.1.dev0
Finished processing dependencies for TimeSeriesAlgorithms==0.1.dev0

,然后当我尝试运行一些代码时,出现以下错误:

(time_series_algorithms_venv) [lpuggini@machinelearn-1 src]$ python time_series_algorithms/examples/example_percentage_variation.py
Traceback (most recent call last):
  File "time_series_algorithms/examples/example_percentage_variation.py", line 2, in <module>
    from time_series_algorithms.percentage_variation import PercentageVariationAD, PercentageVariationADConfig
ImportError: No module named percentage_variation
(time_series_algorithms_venv) [lpuggini@machinelearn-1 src]$ 

这是我的setup.py文件

(time_series_algorithms_venv) [lpuggini@machinelearn-1 src]$ cat setup.py 

from setuptools import setup, find_packages

setup(
    name='TimeSeriesAlgorithms',
    version='0.1dev',
    packages=find_packages(),
)
(time_series_algorithms_venv) [lpuggini@machinelearn-1 src]$ 

我不知道为什么导入失败。我正在使用python 2.7

注意:

我能够导入模块,但不能导入python文件:

(time_series_algorithms_venv) [lpuggini@machinelearn-1 ~]$ python
Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time_series_algorithms
>>> from time_series_algorithms import percentage_variation
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name percentage_variation
>>> 
[

回答评论1: 另外,注释中建议的选项也会失败

(time_series_algorithms_venv) [lpuggini@machinelearn-1 src]$ python time_series_algorithms/examples/example_percentage_variation.py
Traceback (most recent call last):
  File "time_series_algorithms/examples/example_percentage_variation.py", line 2, in <module>
    from TimeSeriesAlgorithms.time_series_algorithms.percentage_variation import PercentageVariationAD, PercentageVariationADConfig
ImportError: No module named TimeSeriesAlgorithms.time_series_algorithms.percentage_variation
(time_series_algorithms_venv) [lpuggini@machinelearn-1 src]$ 

1 个答案:

答案 0 :(得分:2)

这是OP具有的文件名的简单错字:

文件名为:

time_series_algorithms/percentage_varation.py

,导入语句为:

from time_series_algorithms.percentage_variation import PercentageVariationAD, PercentageVariationADConfig

因此文件为varation,导入文件为variation

这在IDE中可能是可以观察到的,或者python脚本本身的痕迹表明它即使看上去已经存在也无法打开percentage_variation.py文件。