Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:35:20+00:00 2026-05-31T10:35:20+00:00

I am attempting to import a python file(called test.py that resides in the parent

  • 0

I am attempting to import a python file(called test.py that resides in the parent directory) from within the currently executing python file(I’ll call it a.py). All my directories involved have a file in it called init.py(with 2 underscores each side of init)

My Problem: When I attempt to import the desired file I get the following error

Attempted relative import in non-package

My code inside a.py:

try:
    from .linkIO can_follow # error occurs here
except Exception,e:
     print e
print success

Note: I know that if I were to create a file called b.py and import a.py(which in itself imports the desired python file) it all works, so whats going wrong?

For eg:

b.py:

import a
print "success 2"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:35:22+00:00Added an answer on May 31, 2026 at 10:35 am

    As stated in PEP 328 all import must be absolute to prevent modules masking each other. Absolute means the module/package must be in the module-path sys.path. Relative imports (thats the dot for) are only allowed intra-packages wise, meaning if modules from the same package want to import each other.

    So this leave you with following possibilities:

    1. You make a package (which you seem to have made already) and add the package-path to sys. path

    2. you just adjust sys.path for each module

    3. you put all your custom modules into the same directory as the start-script/main-application

    for 1. and 2. you may add a package/module to sys.path like this:

    import sys
    from os.path import dirname, join
    sys.path.append(dirname(__file__))  #package-root-directory 
    

    or

    module_dir = 'mymodules'
    sys.path.append(join(dirname(__file__), module_dir)) # in the main-file
    

    BTW:

    from .linkIO can_follow
    

    can’t work! The import statement is missing!

    As a reminder: if using relative imports you MUST use the from-version: from .relmodule import xyz. An import .XYZ without the from isn’t allowed!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I import a file that is in a parent directory within a
From the Django shell (manage.py shell), when attempting to import a python module that
I am attempting to call a C++ API from python. Following is the code
I'm attempting a pretty cut & dry example of anydbm: #!/usr/bin/python import anydbm #
Attempting to print out a list of values from 2 different variables that are
Attempting to make a NSObject called 'Person' that will hold the login details for
I'm desperately attempting to load a video file in OpenCV in Python (on OSX
I have a python script that parses an XML file that contains part information
I am running python 3.1.4 from macports and I am attempting to use the
I am attempting to determine the size of a downloaded file in python before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.