Why does module.__file__ return None?

Question:

I’ve added a local directory to my system path. When I import it, it’s fine but when I do

import local_repo 
print(local_repo.__file__) 

it returns None.

How do I get it to return the path…

P.S. When I try this with other modules works fine – returns the path – for example

import pathlib 
print(pathlib.__file__)

>>>> "C:Python38libpathlib.py"
Asked By: Kurtis Pykes

||

Answers:

__init__.py file should solve it.

Answered By: Shlomo Fel

Possibly not an answer to your specific situation, but I had issues with code in a parent package. The code was preventing the child module from initializing properly and the errors were cryptic and pointed at other modules. If you run into this issue, you may want to check other parent and imported modules.

Answered By: Eric Fossum

I had the same kind of problem in win-64 using anaconda, in particular with pandas module.
My problem appeared when ‘DataFrame’ attribute was called trough an Spyder script. The lower vs upper case issue was not a problem in my case.
I tested from ipython the following properties of the pandas module imported

import pandas
pandas.__file__

The ouptput was

None

I’ve installed in anaconda, in the base(root) enviroment.
then

import pandas
pandas.__file__
Out[2]: 'C:\Users\Usuario\anaconda3\lib\site-packages\pandas\__init__.py'

also, testing the dir() function to analyze and observe the list of attributes inside the imported module

dir(pandas)
Out[3]: 
['BooleanDtype',
 'Categorical',
 'CategoricalDtype',
 'CategoricalIndex',
 'DataFrame',
 'DateOffset',
 'DatetimeIndex',
 'DatetimeTZDtype',
 'ExcelFile',
 'ExcelWriter',
 'Flags',
 'Float32Dtype',
 'Float64Dtype',
 'Float64Index',
 'Grouper',
 'HDFStore',
 'Index',
 'IndexSlice',
 'Int16Dtype',
 'Int32Dtype',
 'Int64Dtype',
 'Int64Index',
 'Int8Dtype',
 'Interval',
 'IntervalDtype',
 'IntervalIndex',
 'MultiIndex',
 'NA',
 'NaT',
 'NamedAgg',
 'Period',
 'PeriodDtype',
 'PeriodIndex',
 'RangeIndex',
 'Series',
 'SparseDtype',
 'StringDtype',
 'Timedelta',
 'TimedeltaIndex',
 'Timestamp',
 'UInt16Dtype',
 'UInt32Dtype',
 'UInt64Dtype',
 'UInt64Index',
 'UInt8Dtype',
 '__all__',
 '__builtins__',
 '__cached__',
 '__deprecated_num_index_names',
 '__dir__',
 '__doc__',
 '__docformat__',
 '__file__',
 '__getattr__',
 '__git_version__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 '_config',
 '_is_numpy_dev',
 '_libs',
 '_testing',
 '_typing',
 '_version',
 'api',
 'array',
 'arrays',
 'bdate_range',
 'compat',
 'concat',
 'core',
 'crosstab',
 'cut',
 'date_range',
 'describe_option',
 'errors',
 'eval',
 'factorize',
 'get_dummies',
 'get_option',
 'infer_freq',
 'interval_range',
 'io',
 'isna',
 'isnull',
 'json_normalize',
 'lreshape',
 'melt',
 'merge',
 'merge_asof',
 'merge_ordered',
 'notna',
 'notnull',
 'offsets',
 'option_context',
 'options',
 'pandas',
 'period_range',
 'pivot',
 'pivot_table',
 'plotting',
 'qcut',
 'read_clipboard',
 'read_csv',
 'read_excel',
 'read_feather',
 'read_fwf',
 'read_gbq',
 'read_hdf',
 'read_html',
 'read_json',
 'read_orc',
 'read_parquet',
 'read_pickle',
 'read_sas',
 'read_spss',
 'read_sql',
 'read_sql_query',
 'read_sql_table',
 'read_stata',
 'read_table',
 'read_xml',
 'reset_option',
 'set_eng_float_format',
 'set_option',
 'show_versions',
 'test',
 'testing',
 'timedelta_range',
 'to_datetime',
 'to_numeric',
 'to_pickle',
 'to_timedelta',
 'tseries',
 'unique',
 'util',
 'value_counts',
 'wide_to_long']
Answered By: Lgal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.