How do you specify a certain path for the import thing to import?

such as import "E:\Tom's Personal Project\engine\shared.py" Any help would be appreciated :)

Recommended Answers

All 7 Replies

Refer here for details on how to make a "Package" taking advantage of __init__.py files... should get you going

i tried what it said on the site but it didn't work
error:
Traceback (most recent call last):
File "E:/Tom's Personal Project/engine/world/worldloader.py", line 6, in <module>
import shared.print.newPrint
ImportError: No module named shared.print.newPrint

folders:

Tom's Personal Project   
    engine
        world
            WorldLoader.py
            WorldLoader.ini
        shared
            __init__.py
            print
                __init__.py
                newPrint.py

my code:

import shared.print.newPrint

Any help would be apprecieted :)

Where are you running your script from? Try os.getcwd() at the beginning of your script to make sure you're actually in the TPP/engine directory.

i am running the worldloader.py file

Try os.getcwd() at the beginning of your script to make sure you're actually in the TPP/engine directory.

Unless you're actually running the script from within the TPP/engine directory you'll need to add it to your path. You can do that at the beginning of your script like so:

import sys

sys.path.append('/home/TPP/engine')
# Rest of your code....
commented: thanks for a good solution +12

Thanks now it works!

The temporary sys.path.append() specific to your program is probably the best solution in your case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.