Reputation: 745
Currently have two repos cloned onto VSCode. When I open VSCode my directory structure looks like this:
- Repo1
- Base 1
- Scripts
-Code.py
- Repo2
- testcode.py
This one python file that I am trying to execute has the following import statements:
from repo2.testcode import testmodule
However, python is not recognizing this as a module... and the following error message is being returned:
ModuleNotFoundError: No Module named 'Repo2'.
I did some research and realized that I need to do either: Do I need to make a package? Or do I need to add to the module path import statement?
Thanks in advance.
Upvotes: 0
Views: 2478
Reputation: 1511
If you decide to use packages, you can specify the path to another local package in setup.py.
Your overall structure would be:
|-- Repo1/
| |-- setup.py
| |-- Repo1/
│ | |-- __init__.py
| | |--Code.py
| |-- tests/
| | |--test_Code.py
|
|-- Repo2/
| |-- setup.py
| |-- Repo2/
│ | |-- __init__.py
| | |--testcode.py
Repo1/setup.py.
from setuptools import setup, find_packages
import os
# dynamically determine the path to Repo2
local_name = "Repo2"
local_path = os.getcwd().split(os.sep)
local_path = os.sep.join(local_path[0:local_path.index(local_name)])
local_path = os.path.join(local_path, local_name)
setup(
name="Repo1",
version="1.0.0",
description="First Repo",
python_requires=">=3.5.0",
packages = find_packages(),
install_requires=[
'SomePyPIPackage',
f"{local_name} @ file://localhost/{local_path}#egg={local_name}"
]
)
The setup.py file in Repo2 would be similar, but wouldn't have the install_requires localhost portion.
Then when you are in the top level directory for Repo1 you can run (don't forget the dot at the end):
# would have to reinstall if you make changes to Repo2
pip install .
# or install in editable mode
pip install -e .
Then in Repo1 you should be able to use:
from repo2.testcode import testmodule
Upvotes: 1
Reputation: 8431
Problem:
ModuleNotFoundError: No Module named
This means the python interpreter could not find this package under the PYTHONPATH or the package was damaged.
You can get the PYTHONPATH
through this code:
import sys
from pprint import pprint
pprint(sys.path)
You can check whether you can find the parent folder of the Repo2 folder.
Solution:
VSCode provides a method to modify the PYTHONPATH, but only works in debugging mode:
"env": {"PYTHONPATH":"${workspaceFolder}"}, //${workspaceFolder} means the path of the folder opened in VS Code. Modify it to the path of the parent folder of Repo2.
Or modify the path manually in the python file:
sys.path.append("parent folder path of the Repo2 folder")
It should be placed ahead of the import statement.
Upvotes: 0
Reputation: 1163
Use the below code before the import statement
import os,sys,inspect
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)
It will take you to the parent directory and will import the module from there
Upvotes: 0