Python Forum
Problem with importing Python file in Visual Studio Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with importing Python file in Visual Studio Code
#8
Example folder setup:
G:\div\
  Menu\
    |-- launch.py
In lanuch.py
print('i am launch')
Let say i have this code in VS Code ,and i am in G:\div folder.
import sys
import os

#print(sys.path)
print(os.getcwd())

import Menu.launch
g:\div
i am launch
Now the import work because current working directory cwd is always added to sys.path
Let say i am in other random folder,eg C:\bar
import sys
import os

#print(sys.path)
print(os.getcwd())

import Menu.launch
Output:
c:\bar Traceback (most recent call last): File "c:\bar\ren.py", line 7, in <module> import Menu.launch ModuleNotFoundError: No module named 'Menu'
Now it dos not work,i have to add G:\div(not launch) to sys.path so Python can find it when in bar folder.
import sys
import os

#print(sys.path)
print(os.getcwd())
sys.path.append(r'G:\div')

import Menu.launch
c:\bar
i am launch
Reply


Messages In This Thread
RE: Problem with importing Python file in Visual Studio Code - by snippsat - Jun-15-2022, 12:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem Converting Tradingview Indicator to Python code kralxs 1 157 10 hours ago
Last Post: kralxs
  Python Coding in Power BI Visual vjrans 0 280 Jan-31-2024, 07:54 AM
Last Post: vjrans
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,053 Nov-15-2023, 06:56 PM
Last Post: jst
  How to set PYTHONPATH in Visual Studio Code? aupres 5 4,262 Aug-15-2023, 03:51 PM
Last Post: snippsat
  how do I open two instances of visual studio code with the same folder? SuchUmami 3 900 Jun-26-2023, 09:40 AM
Last Post: snippsat
  Visual Studio Code NewPi 3 1,067 May-16-2023, 11:13 PM
Last Post: snippsat
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 731 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  python standard way of importing library mg24 1 916 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Visual Studio Code venv ibm_db error mesi1000 7 2,838 Nov-13-2022, 12:36 AM
Last Post: snippsat
  Problem with importing python-telegram library into the project gandonio 1 1,589 Nov-01-2022, 02:19 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020