Python can't find module when using interpreter - python

When writing a program, I like to use the interpreter first to execute my code line by line. However, when using the interpreter, it can never seem to find modules that are definitely installed.
There is no issue when running a .py file. It's only when using the interpreter. My only solution is to change directory to where the module resides, but this is just so time consuming.
What could be the issue here? I wouldn't be anything to do with permissions as it works fine when run with a .py file. Not sure if it's a path environment issue, because again the issue is only when executed in the interpreter. It's very weird.

Try installing an older version of python and then see if you get the same error. It worked for me when I had the same error.

Related

Python inside a virtualenv is broken (Django)

TL;DR - Microsoft Store Apps are broken (0 bytes), hence the Python interpreter is unable to create process and run "Python" inside virtualenv, I failed to follow numerous explanations of how to change virtualenv path for Python.
Recently, without any changes to my computer/environment, an issue started occurring when executing (also tried python3, which brings the same):
python manage.py runserver
This brought back the following issue:
Unable to create process using
'"C:\Users\MyUser\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe"'
As I dug deeper, I realized that all of the apps installed under this folder are 0 bytes, hence they are completed broken, all of a sudden.
Therefore I figured it's not an issue with Django, rather the python itself.
I tried changing the virtualenv path for Python.exe, instead of using WindowsApps version, I installed the original Python from the original website.
I failed to do so, multiple times.
I tried: "https://stackoverflow.com/questions/4757178/how-do-you-set-your-pythonpath-in-an-already-created-virtualenv/47184788#47184788" - Adding the path inside "activate" and "activate.bat", as
set PYTHONPATH="C:\Users\MyUser\AppData\Local\Programs\Python\Python310\python.exe"
And the issue persists.
I tried every solution/article I found in regards to the issue I have. Many of them claim a simple result, whereas the rest claim a complete refactor is required.
Even downloading from Microsoft store is broken, it always fails.
Since I'm unable to remove the broken apps, and I wasn't able to find a way to change the virtualenv Python interpreter, I am here, asking for your assistance.
Thanks in advance,
Since you have installed python from python.org, ensure that it has been properly added to the PATH and you're not inadvertently using the windows store version.
Open command prompt
Execute where python to check which python your system is using.
If you're only seeing the WindowsApps version of python listed in the output of where command, then python installer apparently didn't add the location to PATH, so you'll need to manually add it yourself.
Also, if you're not restricted to use virtualenv, you can give conda a try.
As I began rebuilding, I figured it out.
When building a virtualenv, the python I used was directing to:
"C:\Users\MyUser\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe"'
Python interpreter is saved as the "PATH" to execute python.
Since my Python interpreter from Microsoft Store went broke, I was unable to execute python inside the virtual environment.
Venv configures a file called:
pyvenv.cfg
This is how the virtual environment knows what Python interpreter to use.
Inside it, the first line states:
home = C:\Users\MyUser\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\
Therefore, we just need to re-configure it to the new path of Python interpreter:
home = C:\Users\MyUser\AppData\Local\Programs\Python\Python310
And it worked for me.
Thanks for everyone's assistant, glad it's done.

Python on VSCode not Running on the Selected Environment

I have a bit of a weird issue that I couldn't find an answer to on Google. I'm trying to use Python on VSCode (quite new to it, I've been using PyCharm till now), but for some reason when I try to import and use Numpy or PyTorch, it throws a ModuleNotFoundError (see below).
I've double checked to ensure I've selected the right environment and that Numpy and PyTorch are both installed in this specific environment.
This code also works without issue in this environment if I try it on PyCharm, so the issue seems to be entirely in VSCode alone. Note, I'm using the Code Runner extension by Jun Han to run this.
Please help! Thank you.
Thanks all for your suggestions. Reinstalling everything (Conda and VSCode, as well as PyTorch) fixed the issue for me.
The file is located on your desktop but the file should be somewhere where it has access to "python\Lib\site-packages" folder where numpy should be installed.
1.go to your folder where python is installed.
2.go to the folder called "scripts" and make a .py file in there, try to run the same code and if the "ModuleNotFoundError" isn't fixed yet.
3.reinstall numpy from the terminal in the file thats located in the scripts folder. I had the same issue but putting my file into the "scripts" folder fixed it for me.
If this dosent work just tell me. :^)

Can't use any Atom Python IDE functions

I am trying to setup Atom as a Python IDE.
I have installed atom-ide-ui, and ide-python packages. Python and pyls are installed as well to the latest versions. Nonetheless I am not able to use any of the functions the packages should provide (e.g. autocomplete, highlighting, etc.), they just do not seem to be active.
I have tried to set the python-ide Python Executable path to the actual install path (C:\Users\user\AppData\Local\Programs\Python\Python38-32\python), as I though it could be a problem with the defaults. Still nothing.
I am wondering if I am missing any import step in the setup, or if I am using something wrongly.
Thanks in advance.
Did you load the language-python extension? Its necessary to run Python. Was your file in color after opening a valid python code?
You should also load the extension named Script. Script is required to run your python file. after you start atom, under the Packages menu you'll see the word Script. Click on this and you'll get another pane that says run script. Click on this to run you python code/

Python commands are not executing in cmd.exe

I have installed Python 3.8. It is working fine when I open the EXE file, but when I try to run it from command line it is not working.
For example, I tried to find its version in cmd, but I think it is not executing.
I am not sure, but maybe you could try declaring a Python path in the environment or global variable.
Check out the article Add Python to the Windows path with a resolution to the problem.

Visual Studio Code not finding module

I am trying to debug a PYTHON software using visual studio code, I have a problem with one module.
This module is located in a folder, on my hard drive. When I try to run the program, I get the following error:
from toto import tata
ImportError: No module named toto
I do not really know what to do with this, the path to the module is in my windows environment variable PythonPath, and for some time, it was working well with VSCode and I could debug the module with no problem.
Moreover, when I call python directly with a command line:
C:\Python27\python.exe start.py
It works perfectly, which leads me to believe that python has no actual problem finding the module.
My VSCode is correctly set up to use python27 as my interpreter.
Late solution: It was a bug in VSC causing it to incorrectly read the PYTHONPATH variable. The workaround was to create an empty .env at the root of the workspace save folder. I think this workaround may be obsolete now.

Categories