Why can't I import my files in VSCode in python? - python

This morning I tried to import a file that I created into another file but the file name that I am trying to import has the yellow scribble line under it. I know this is an import error but I can't fix it. I am working in VSCode on Windows 10 in python. The files are in the same directory.
I tried to import the path of the file but it doesn't work.
I had an old working file which requires me to import another file into it but now it doesn't work as well. Is this a new bug in VSCode?
Any help would be appreciated.

you can try : path/to/python3 -m path/to/script.py
or just specify the full path : 'C://Users/quina//...//word.txt'
Maybe try to use the normale python3 IDLE or throught the terminal just to understand you error.
There is no word.txt in 'Visual Studio Projects' folder

Related

Python Pyinstaller build fails using win32com package [duplicate]

I am trying to convert my .py file to an .exe file and I have tried all methods (auto-py-to-exe, pyinstaller, cz_freeze) and it does create the exe file but it always gives an error or the window opens and closes as soon as I double click the file.
It is a SpeechRecognition AI project coded in python. And it works just fine in the IDLE but once I create the .exe and try to run it the window pops up and shut down immediately after. (I use the cx_freeze and setup.py method for this)
If I try to convert .py to .exe using pyinstaller it gives me several different kinds of error messages.
As a .py file it works just fine but it doesn't work as an exe.
This is the error I get when using pyinstaller or auto-py-to-exe: Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path
Module 'pythoncom' isn't in frozen sys.path
I tried several things but nothing seems to work. I was previously using Python3.10 so I uninstalled it and downgraded to Python3.8 and reinstalled all the modules so technically it should work. I tried to create .exe files of another project and it worked just fine.
Another issue I come across is ModuleNotFoundError: No module named 'pyttsx3.drivers' I compiled the .exe using cx_freeze and it did create an .exe but it gives me this error.
Could someone please help me out with this?
(PS: This is the list of imports I am using for this project:
screenshot of all imports
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import wikipediaapi
import webbrowser
import os
import time
import subprocess
import wolframalpha
import json
import requests
from newsapi import NewsApiClient)
I just solved this for my project, which is using none of the direct imports your project is, but it appears the same low level library is causing an issue.
In my case, the single import is:
from pywinauto.application import Application
The solution is to tell PyInstaller to bundle the missing .DLL, which in this case is pythoncomxx.dll where xx is your python version. 39 in my case.
Here is the call that eventually worked for me.
pyinstaller --onefile --add-binary ".venv/Lib/site-packages/pywin32_system32/pythoncom39.dll;." autoTest.py
More generally: pyinstaller --onefile --add-binary "[path to dll];." file.py
Note, I'm on Windows. If you are on mac/linux, the ; character in the --add-binary argument would be :. See note in the documentation here
Discussion
This clicked for me when I used ProcMon to profile the file access of my initial version that was failing. It was looking for this .DLL in a bunch of different folders, then quitting.
The answers to this question were helpful. But I didn't want to settle for copying the DLL to output manually. And consequently, having to use a directory output versus a single executable file.
Answers on this question also gave the hint about using the --add-binary flag, but ultimately I had to provide the specific path to the missing DLL instead of just referencing by name as these answers showed. It probably works to specify the DLL by name if it is accessible on your PATH.
How to locate this DLL in the first place? Do a search on your site-packages folder.

Code not able to locate file from within the same path as the program itself

My program is located under C:\Users\Username\pycode.py
My file to be called within the program is located under C:\Users\Username\<filename>
The file and the code are in the same directory.
When I call from within the program using the below set of codes,
import os; import sys
with open(os.path.join(sys.path[0], "file1.txt"), "r") as fn1:
print(fn1.read())
the Py code picks up the file and executes the applied methods on it.
However, when I don't use the above code and use as:
fn1 = open("file1.txt", "r")
print(fn1.read())
There is only an error that the file does not exist.
Any thoughts and ideas, what is missing to make the file within the local directory as the py code to recognize the file.
If it's relevant, I'm a beginner using VSCode on Windows 10.
Speaking from personal experience, it happens to me when I am using vscode and I am in a directory say d:/Desktop/python programs. But the file I want to open() is in the directory d:/Desktop/python programs/fun.
In this case it is not sufficient for me to use the path like "filename". Instead I need to use "/fun/filename". This is even though the file I am working on is in the fun folder.
So perhaps you could go to the terminal in VSCode (your IDE) and run cd "path to your file" and that should solve the problem
Try running the following code and see what output you get
import os
print(os.getcwd())
In my case this would print d:/Desktop/python programs

Calling a python module from a .py file

I am a novice and I am trying to follow this code: https://github.com/llSourcell/autoencoder_demo/tree/master
Now, as you can see in the picture, the main file throws a no module found error. The question I have is how can I import the input_data found in the adjoint file?
You can do the following to fix the problem.
Extract the files to some folder. Open a command prompt from that folder and then run the script. Make sure that you run cmd from the directory where files are. This is because, python looks at the current directory and Python path, when you try to import a module.

Python import error in Jupyter Notebook Pycharm 2019 IDE

I recently installed PyCharm 2019 professional edition in Windows 10. I created a new Project 'Sample' and two files 'file1.py' and 'file2.ipynb'. I have installed jupyter notebook from the chosen python interpreter.
class Foo is defined in file1.py
I then import file1.py into file2.ipynb to use Foo
Here, I encounter a strange error. 2019 professional edition has local notebook server installed in it.
from file1 import Foo
I ran the above code sample in a cell both in editor inside PyCharm IDE as well as in browser, after turning on the jupyter server on localhost.
For some reason, the code sample throws out import error problem in IDE but runs smoothly in browser. I tried looking if there are any issues with project path but couldn't figure out the reason yet. Anyone encountered this before ?
Here is the screenshot of Import Error:
os.getcwd() helped me understand the problem. The jupyter notebook editor is running under Main directory 'Sample'. Whereas my files are in 'Sample/Resource/file1.py', 'Sample/Resource/file2.ipynb' .
Now If I import using the following commands in file2.ipynb file, it worked:
import os
print(os.getcwd())
#%%
from Source import file1
print(file1.Foo())
Thank you very much for the help #Vishal #IonicSolutions

import statement is not working when running python script from the command line

I need to run a python script from the command line (OS = Debian wheezy, python -version 3.5).
I used PyCharm (community edition) to write the script and it is working from inside the IDE.
I used sys.path.append command to add the directory containing the package I want, then followed it with this import line:
from package_name,file_name import ClassName
The Error message in the command line:
ImportError: No module named 'package_name'
if you are running any xxx.py file and you face the import error though same script if run by any IDE works fine,its path issue.
What worked fine for me is:
Go to file which shows import module issue and before importing module(for which issue is seen),add the path of module to sys using append.
for example ,I was running the script file from conf path and my script was importing module situated in \scripts\Setup\ so appended the path of module like below.
import sys
import os
conf_path = os.getcwd()
sys.path.append(conf_path)
sys.path.append(conf_path + '\scripts\Setup')
then use import statement of module for which issue was thrown.
I found the answer for my question above, and the problem was much easier than I thought.
Addressing the Problem
having many python packages in different directories
your script needs some/all packages, that are not in the standard lib-directory for your python installation (e.g.:prefix/lib/pythonVersion).
Solution
Short term solution
As long you are using an IDE (e.g. PyCharm), it is sufficient within the code to add:
import sys
sys.path.append("path/to/package")
As soon as you have to run your script from the command line, you will get an ImportError as mentioned in the Question above.
Better solution
Add the directories of your packages and of your python installation to your shell-profile(e.g.: .bashrc) using the command:
export PYTHONPATH=prefix/lib/pythonVersion:/path/to/packages
To get more info about PYTHONPATH, check this link
In this case you will not need to append the path of your packages within your code :)

Categories