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

  • SEARCH
  • Home
  • 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 8780893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:10:12+00:00 2026-06-13T20:10:12+00:00

Why does my namespace import not work when I use the os.chdir() to get

  • 0

Why does my namespace import not work when I use the os.chdir() to get to the package dir from script?

For example here is my package structure to demonstrate the problem.

testpk/
testpk/bin
testpk/bin/runit.py
testpk/lib
testpk/lib/libcode.py
testpk/lib/__init__.py

here is my lib code which just prints ive been imported

print "I've been imported"

contents of runit.py

#!/usr/bin/python
import  sys, os

if __name__ == "__main__":
    os.chdir('/home/moorepe/src/testpk')   
    print "working path = " , os.getcwd()
    import lib.libcode 

If i run runit i get this error:

moorepe@halifax$ bin/runit.py 
Traceback (most recent call last):
  File "bin/runit.py", line 6, in <module>
    import lib.libcode 
ImportError: No module named lib.libcode

However testing this with python command line it works as expected:

    cd testpk
    python -c "import lib.libcode
    I've been imported

And this works from the bin dir:

cd testpk/bin
python -c "import os; os.chdir('/home/moorepe/src/testpk') ; import lib.libcode"
I've been imported

Can anyone explain what is going wrong?

So the reason this didnt work is that python uses the current dir as a path in addition to the PYTHONPATH.

so this code works by adding sys.path.append(“.”) because im forcing a current working dir as a path addition.

  • 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-06-13T20:10:14+00:00Added an answer on June 13, 2026 at 8:10 pm

    If you add a '.' for the current directory to the sys.path search folder list, then the os.chdir() will become effective and the import lib.libcode should start working:

    File runit.py:

    #!/usr/bin/python
    import  sys, os
    
    if __name__ == "__main__":
        sys.path[0:0] = ['.']  # prefix current directory to list
        os.chdir('/home/moorepe/src/testpk')
        print "working path =", os.getcwd()
        import lib.libcode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have an example of setting up Authlogic with a namespace in Rails?
Does anyone know a script which would wrap old Products namespace style Plone add-on
Why one project (exe) does not see the namespace of another project (dll) in
It's quite often to see com.* package namespace. What does 'com' mean? Thanks.
using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; VS says, The type or namespace name 'Formatters' does not
Why does Microsoft.JScript work in the Code behind but not within a <% %>
Consider following two examples: Example 1: <xs:import namespace=http://example.com/ns schemaLocation=test.xsd/> Example2: <sample:Data Test=true xsi:schemaLocation=http://example.com/test.xsd> How
Microsoft introduced a new System.IO.MemoryMappedFiles namespace in .Net 4. Does anyone know if it
When its giving an error saying AppHelper does not exist, am I missing any
Suppose somewhere I import javax.servlet.http.HttpServlet . My questions: Does this mean: I could find

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.