Skip to content
Fix Code Error

Importing modules from parent folder

March 13, 2021 by Code Error
Posted By: cool-RR

I am running Python 2.5.

This is my folder tree:

ptdraft/
  nib.py
  simulations/
    life/
      life.py

(I also have __init__.py in each folder, omitted here for readability)

How do I import the nib module from inside the life module? I am hoping it is possible to do without tinkering with sys.path.

Note: The main module being run is in the ptdraft folder.

Solution

It seems that the problem is not related to the module being in a parent directory or anything like that.

You need to add the directory that contains ptdraft to PYTHONPATH

You said that import nib worked with you, that probably means that you added ptdraft itself (not its parent) to PYTHONPATH.

Answered By: hasen j

Related Articles

  • What is the worst programming language you ever worked with?
  • Error message "Forbidden You don't have permission to access…
  • Can't install via pip because of egg_info error
  • What's the difference between eval, exec, and compile?
  • How can I use a Python script in the command line without…
  • Vue js vuecli3 application does not work in ie11 (babel is…
  • Visual Studio Code pylint: Unable to import 'protorpc'
  • Aurelia UX showcase app fails to load
  • adding directory to sys.path /PYTHONPATH
  • Relative imports for the billionth time

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Regex to check whether a string contains only numbers

Next Post:

How can I parse a JSON file with PHP?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error