PyCharm Run Doesn't Recognize Packages But Console and Terminal Are OK - python

Running PyCharm 2020.1.2 Community Edition in Win10 with a Python 3.6 venv as interpreter. Installed the package feature-engine through the Project Interpret interface, installs fine and appears in the list. I can successfully import feature_engine in the PyCharm console, and I can use it fine. I can also execute a .py file with this import statement in the Terminal with the venv activated, and it also works fine. However, when I try to Run the same .py file with the import statement, I get:
ModuleNotFoundError: No module named 'feature_engine'
I have tried using import and importlib, thinking the issue was the hyphen, but those didn't work. I have tried uninstalling and reinstalling, restarting PyCharm, etc. Nothing seems to work. Any suggestions how to get the Run function working?
EDIT: Thanks for the suggestions. Attached are the Run configuration and the Project interpreter configuration. As far as I can tell, the environment is the same.
Below are examples of the error trace. The object being Run is a Flask app, which imports packages that use the feature-engine library. The actual import statement in the final import is simply import feature_engine. Trying to import the method directly using from feature_engine import variable_transformers as vt also fails.

Make sure you're using the right configuration to build your program (that is, using the python executable of the right enviroment). You can check this in the top right corner, where the run button is.

Related

Module can be imported in python but cannot be used from command line

This is essentially the opposite of this problem: Import module works in terminal but not in IDLE
If I start a python session and try to import flask, then it works just fine. Despite that, if I just run flask from the terminal, it is not recognized.
I have confirmed that the locations which pip installs to is on my system path. (I'm on Windows 10 and I typed $Env:Path into Powershell to see that, for example, C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages is in the system path.)
I am also sure that I did pip install --user flask not inside a virtual environment. When I run import flask; print(flask.__file__ I see C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages)
Any other ideas for what could be going wrong?
Edit: I'm trying to run flask run. To check the path python uses, I did (in python) import sys; for i in sys.path: print(i)
As Python language based on Python interpreter, before you use Python, it is necessarily to activate the Python env.

VS code cannot import local python modules

All of my project files in VS code suddenly gives an error saying that it cannot import modules (even tho the modules are local i.e same directory and they used to work pretty well before).
The code works fine in pycharm but not in VS code, any idea whats going on?
Code:
from backend.util.crypto_hash import crypto_hash
from backend.config import MINE_RATE
error:
env DEBUGPY_LAUNCHER_PORT=34625 /home/nikhil/python-blockchain/blockchain-env/bin/python /home/nikhil/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher /home/nikhil/python-blockchain/backend/app/__init__.py
Traceback (most recent call last):
File "/home/nikhil/python-blockchain/backend/app/__init__.py", line 2, in <module>
from backend.blockchain.blockchain import Blockchain
ModuleNotFoundError: No module named 'backend'
Close VS Code,
start it again, Go to File > open folder (open your project folder in vs code),
if it gives a prompt to select a existing virtual environment, select that.
Then you should be good to go.
More unrelated information:
I assume the issue here is that there's some problem with VS Code not recognizing the virtual environment properly. This has happened to me several times and I cannot point out why that happens. But the above solution is a quick fix and always works for me.
I'm not sure if this will resolve OP's (ten-month old) question, but I've been struggling with the same error using debugpy while trying to configure VSCode's debugger.
Step 4 of this resource resolved the issue for me. In particular, I was using the -m flag when running debugpy, but the module to be run was not in the current working directory. Once I had changed this, the debugger worked as expected. As an example, if the command was originally:
python -m debugpy --listen 0.0.0.0:5678 ./some/directory/my_script.py
then the following two commands would rectify it:
cd ./some/directory
python -m debugpy --listen 0.0.0.0:5678 ./my_script.py
After doing this, I no longer received the "No module found" error.
The main reason is that VSCode does not automatically configure environment variables for you, but PyCharm does. The simplest method is adding your module file to system path.
import sys
sys.path.append(module_file_path)
The better solution is to config your environmental path named PYTHONPATH, adding related module path to it, and after that you will no longer need import system path manually.
Hope it works!

Module Not Found in python script

I'm a python beginner, and I'm following this tutorial for a webscraper https://hackernoon.com/building-a-web-scraper-from-start-to-finish-bb6b95388184
I'm on Windows 10, have setup a venv, activated and installed 2 modules using pip, and moved my script into the Scripts folder (from my understanding, this is the equivalent of the /bin/ folder on linux installations). The modules are bs4 and requests. I see both of these in the /Lib/ folder of my venv. I am using the Atom editor from atom.io, and the Scripts Package to run my script.
My script errors with a "module not found" error. Relevant snippet below:
scraper.py
from bs4 import BeautifulSoup
import requests
I get the error on both imports, indicating I've setup my project/imports incorrectly. I have no shebang line in my script, and suspect this is the problem.
My project structure looks like:
\ScraperProject
|-\ScrEnv
|-\Include
|-\Lib
|-\site-packages
|-\bs4
|-\requests
|-\Scripts
|-scraper.py
|-pyvenv.cfg
What is the proper way for me to import these modules into my script in a Windows environment?
with what python version are you working in the venv? Maybe try to uninstall those update pip and reinstall those again, making sure that you install them with pyhton3
My issue is related to running it from the Atom editor, something I hadn't considered before. It's calling the python executable from my PATH variable, and not the one in my venv. When running the script from the cmd window, and calling it with the python from my project folder/venv, it runs as expected. On to figuring out how to configure Atom to use the venv executables. Thank you very much for the interest and help!

Suddenly .py files can't import modules but still works with CMD

I have a weird issue.
Main issue:
My .py files that used to work fine like 3 hours ago now can't import any external modules. I can still run them from Spyder (similar to a PyCharm editor) and from CMD with python run.py. However when clicked on I get the error ModuleNotFoundError: No module named ModuleName. However the module is found when running through everything else, the module is there in the Anaconda libs; the folder doesn't have any permisson restrictions, and it's not just one file it's any .py file that imports an external module.
At first I thought this may be a pip issue as I had just update to pip 18, but even when retracting to pip 10.0.1 the issue remains.
[EDIT]: I've tried making a PyInstaller .exe and that still works as intended, however the app still doesn't work with cx_freeze even though it used to a fez hours ago.
Backstory:
I was playing around with PyInstaller and Cx_Freeze to turn my app into an executable.
I have my working .py file that I edit and test inside of the Anaconda's Spyder app.
And so I'm testing the executables, and they work fine, just like my python code. The Pyinstaller standalone and the cx_freeze app work as intended.
So I change a few things in the main .py file (nothing crazy just removed a print('')), reuse cx_freeze and then at some point I start working on a setup wizard for my cx_freezed app.
It's all good except that when running the app, the cmd prompt just closes.
I think 'huh weird', I test the .py file in Spyder it works fine, so I screenshot what's written on the cmd : ModuleNotFoundError: No module named ModuleName, so I think it's an issue with the Wizard installer, so I try the original .exe file, same error. So I try the .py file and to my desmise, same error. I double check the modules, reinstall them succesfully, error persists.
And so I try to run a backup I know for sure worked and in which I haven't edited anything, and now same error.
This is really anoying as I want to make a .exe of the app, managed to and now nothing works anymore
Here are some things you can try.
Add this code to get a print out of the system path.
import sys
from pprint import pprint
pprint(sys.path)
That should tell you all the paths where modules can be loaded from. If your file is not in one of the paths it won't be loaded.
For a bit more info you can run python with the -v flag and it will verbosely let you know what is going on as python starts as well as when you attempt to load modules. You may be able to glean information about what is going wrong that way.
I think you used the wrong version when you converted from a .py to .exe. Generally the CMD uses whatever's in the ENV vars, so just make sure all version numbers are the same.

how to use relative import within python spyder IDE

I got anaconda python and is using the spyder IDE. I'm trying to figure out how can I use relative import for with the run bottom or F5.
suppose I have pkg/A/foo1.py, pkg/A/foo2/py, and foo1.py has "from . import foo2", if I hit run it will report relative import error.
I know how to do it in the command line environment where I can type, e.g. "python -m pkg.A.foo1". How can I do this in spyder IDE?
Thanks
jq
If there is pkg/A/__init__.py file i.e., if pkg.A is a Python package then from . import foo2 is correct. It doesn't matter where you write your code in the spyder IDE, notepad, or emacs; the code is the same.
The remaining question is how you run Python scripts in spyder IDE.
Don’t directly run modules inside packages i.e., don't run python pkg/A/foo1.py. It leads to Python modules being available under different names. See Traps for the unwary. Run it as python -m pkg.A.foo1 from the project directory instead.
Configure the command that is run on F5 if spyder IDE allows it.

Categories

Resources