Python Forum
Problem with importing Python file in Visual Studio Code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with importing Python file in Visual Studio Code (/thread-37465.html)



Problem with importing Python file in Visual Studio Code - DXav - Jun-14-2022

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.


RE: Problem with importing Python file in Visual Studio Code - deanhystad - Jun-14-2022

Is there a "launch.py" file in the current working directory? Is it a Python file?


RE: Problem with importing Python file in Visual Studio Code - Larz60+ - Jun-14-2022

see: VS Code from start
search for 'After install the first ting to do is install needed Extension.'
add the extensions related to python


RE: Problem with importing Python file in Visual Studio Code - DXav - Jun-15-2022

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


RE: Problem with importing Python file in Visual Studio Code - rob101 - Jun-15-2022

(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
?


RE: Problem with importing Python file in Visual Studio Code - DXav - Jun-15-2022

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


RE: Problem with importing Python file in Visual Studio Code - rob101 - Jun-15-2022

(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?


RE: Problem with importing Python file in Visual Studio Code - snippsat - Jun-15-2022

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