0

I have a python project structured like this:

root/
    __init__.py
    coolstuff.py
    moduleA/
        __init__.py
        myscript.py

When my working directory is moduleA, how can I run myscript.py, knowing that it needs to have access to coolstuff ?

EDIT: I am aware of the $PYTHONPATH solution but I would like to know which other solutions exist

5
  • 3
    you can add your root directory to $PYTHONPATH & you can then import coolstuff module anywhere.. Commented Feb 8, 2018 at 10:05
  • here's your naswer: stackoverflow.com/questions/72852/… Commented Feb 8, 2018 at 10:21
  • @shiva your answer doesn't take into account the fact that I want to run myscript from the working directory moduleA; doing so will either get me an Attempted relative import in non-package or a Attempted relative import beyond toplevel package Commented Feb 8, 2018 at 10:32
  • @advay.umare feel free to put your comment as an answer as I think it could still be of use to other people Commented Feb 8, 2018 at 10:33
  • @FlorentinHennecker OKZZ Commented Feb 8, 2018 at 10:35

3 Answers 3

1

Include your root directory to path and import your package. Include the following code in myscript.py.

import sys
from os.path import dirname,abspath
sys.path.append(dirname(dirname(abspath(__file__))))
import coolstuff
# do cool stuff

Hope this helps!

Edit: added import sys

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, interesting answer! There is still one thing that bothers me about it though; this solution requires you to know where you are in the hierarchy. Would there be a way to do this by defining root as the top-level package and avoid doing a hardcoded number of dirnames ?
I have no knowledge of such solution, but I don't see a problem in knowing the hierarchy of your own project and including 3 rules at the top of your scripts.
If by run you mean to execute so that it's its own __main__ then no, I do not think so. See: docs.python.org/3.6/tutorial/…. Esp. last paragraph. In other words. Directly executed script has no knowledge of a package the directory structure may suggest it is being part of.
0

You can import sys and import the actual directory where your coolstuff.py exist.

Eg:

import sys
sys.path.insert(0,"/root")
import coolstuff

Note: If you are using Windows use the path like this - "C:\Users\test\Documents" (Considering your coolstuff.py is residing in Documents)

__init__.py is not required for this method.

Hope this help you!

Comments

0

Add path of your project to PYTHONPATH :

export PYTHONPATH =$PYTHONPATH:/xyz/root/

or from python like:

import sys
sys.path.append("/xyz/root/")# path to your project directory

Now you can access all the module from your root directory by simply impporting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.