2

I have the following files. And I want to be able to run python web\file1.py in Mod1.

Mod1 web __init__.py file1.py file2.py __init__.py 

and in file1.py:

import web.file2 

However, when I run the following command in the directory mod1:

....\Mod1> python .\web\file1.py Traceback (most recent call last): File ".\web\file1.py", line 1, in <module> import web.file2 ModuleNotFoundError: No module named 'web' 

I tried to change the content of file1.py to

from . import file2 

and the error become

ImportError: attempted relative import with no known parent package 
3
  • Have you tried just from web import file2? Commented Oct 14, 2020 at 18:53
  • It also got the error of ModuleNotFoundError: No module named 'web'. Commented Oct 14, 2020 at 19:01
  • @napuzba I'm having trouble resolving the issue described, and I don't find your tutorial to provide a resolution. I believe the issue is related to package name resolution in pylance, as if for a package to be imported, and resolution in the interpreter, run as a script. Either way, I'm stuck doing what works, without pylance type hinting, or getting pylance type hints and not being able to run as a script. Commented Jun 14, 2021 at 13:05

2 Answers 2

2

As I understood what You want, You want to have file2.py imported in file1.py

If that is correct, all You need to do is type this in file1.py

import file2 

Happy Coding!

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

2 Comments

Yes, this works. However, the python language server (pylance) of visual studio code warned "file2" is not accessed Pylance Import "file2" could not be resolved Pylance (reportMissingImports)
1

It seem you are trying to import web/file1. Since there is an init file at mod1, you should use this in Mod1

import .web.file1 as f1 

Then this in file2

import .file1 as f1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.