6

I'm trying to import a package from a different project, but it's not recognising the project that I'm trying to import from. I've looked at various answers to this question (including python: import another project module named same with a local module).

My project structure looks like this:

Project1
 - __init__.py
 - foo_directory
  - foo.py
  - __init__.py


Project2
 - __init__.py
 -bar_directory
  - bar.py
  - __init__.py

In bar.py I'm trying to do:

import sys
sys.path.append('path/to/Project2')

from Project1.foo_directory import foo.py

I can't work out why it's not recognising Project1 when I try to do an import?

2

4 Answers 4

5

You should create two packages, Project1 and Project2 (note the setup.py)

Project1
 - setup.py
 - Project1
  - __init__.py
  - foo_directory
   - foo.py
   - __init__.py

and

Project2
 - setup.py
 - Project2
  - __init__.py
  - bar_directory
   - bar.py
   - __init__.py

then install them

pip install -e Project1/
pip install -e Project2/

And then you can simply do

from Project1.foo_directory import foo

The obvious advantage: Package2 depends on Package1 but doesn't need to know where it was installed. Managing all the import paths is done by pip and the environment you're in (hopefully a virtualenv).

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

4 Comments

if i don't put Project2 inside Project 1, i get .. should either be a path to a local project or a VCS url beginning with svn+, git+, hg+, or bzr+ , local package must be inside the parent package to install?
Have you made sure the directory you specified has a setup.py file?
@TomSawyer please open a new question
I got it done. But just to remind that you might need to reopen VS Code to let the editor know.
1

Based on the advice Maresh gave me, I did the following (I'm using Windows, which is why I couldn't follow his answer exactly):

In PyCharm: File > Settings > Project: Project1 > Project Structure. - Add Content Root (add the location for Project2). - Apply and OK

Then in bar.py I was able to import files from Project2

Comments

0

2 Solutions:

1- Use the PYTHONPATH environoment variable. See this answer for more details

$ export PYTHONPATH=$(PYTHONPATH):/path/to/project1 

2 - Create a setup.py for your projects so you can install them, thus import them>

https://docs.python.org/3/distutils/setupscript.html

Note: Messing with the sys.path from within app is not a recommanded solution as it is not really robust not crossplatform.

Comments

0

A workaround for Project2's bar.py to import Project1's foo.py

Adopted from a workaround I've been using myself.

Important: For this workaround to work you may need to disable any format-on-save feature, specifically because of the rule “E402 - Fix module level import not at top of file”

a) either with a hardcoded absolute path to Project1:

(don't forget an absolute path starts with a leading slash)

bar.py:

import os
import sys
sys.path.append(
    os.path.abspath('/abs/path/Project1')
)
from Project1.foo_directory import foo as f

b) or with a relative path from bar.py location to Project1, converted programmatically to an absolute path:

bar.py:

import os
import sys
sys.path.append(
    os.path.abspath(
        os.path.join(
            os.path.dirname(__file__),
            '../../Project1'
        )
    )
)
from Project1.foo_directory import foo as f

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.