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
#1
Hello here ! I'm learning how to code starting with Python, and I'm facing a problem trying to import a file as a module. With this code :

import os
import sys
sys.path.append(os.getcwd()+"\launch")
import launch as l
I'm getting the issue "Import "launch" could not be resolved", and if I execute the script, the error "ModuleNotFoundError : No module named 'launch'". I'm struggling with this for a few hours now, and I'm quite desperate, so thanks in advance for your answers !

Of course, with the command :

os.popen(os.getcwd()+"\launch")
I can open my file, so it's not a problem of wrong path.
Reply
#2
Is there a "launch.py" file in the current working directory? Is it a Python file?
Reply
#3
see: VS Code from start
search for 'After install the first ting to do is install needed Extension.'
add the extensions related to python
Reply
#4
I installed all the needed Extension
There is a "launch.py" file in in the "Menu" folder in my current working directory. This is a python file with only one function for now. I can open it with
os.popen(os.getcwd()+"\launch")
but I can't import it
Reply
#5
(Jun-15-2022, 09:46 AM)DXav Wrote: There is a "launch.py" file in in the "Menu" folder in my current working directory. This is a python file with only one function for now. I can open it with... but I can't import it

Would it not be:
import Menu.launch
?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
Menu.launch is actually proposed by visual code but with

import Menu.launch
I have the error
Error:
ModuleNotFoundError: No module named 'Menu'
but I have a "Menu" folder in my working directory
Reply
#7
(Jun-15-2022, 10:54 AM)DXav Wrote: but I have a "Menu" folder in my working directory

Just to be clear, when you say 'working directory', this is the directory from which the main app is being run?

For example, when I'm developing a script, I 'work' from /home/rob/python/dev/the_app.py

... so any custom modules that I need for said 'app', I create in /dev/modules/
If I've a module called 'test.py' I need to use import modules.test which works just fine.

n.b: I'm using a Linux OS

edit: if the above rings true, then it seems to me that the issue you have is more about VS than Python. Have you asked about this in a VS Forum?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem Converting Tradingview Indicator to Python code kralxs 1 145 1 hour ago
Last Post: kralxs
  Python Coding in Power BI Visual vjrans 0 279 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,052 Nov-15-2023, 06:56 PM
Last Post: jst
  How to set PYTHONPATH in Visual Studio Code? aupres 5 4,253 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 897 Jun-26-2023, 09:40 AM
Last Post: snippsat
  Visual Studio Code NewPi 3 1,064 May-16-2023, 11:13 PM
Last Post: snippsat
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 729 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  python standard way of importing library mg24 1 915 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Visual Studio Code venv ibm_db error mesi1000 7 2,836 Nov-13-2022, 12:36 AM
Last Post: snippsat
  Problem with importing python-telegram library into the project gandonio 1 1,583 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