how to use relative import within python spyder IDE - python

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.

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.

Selenium works in Terminal but not the Python Shell

I am running python on a Mac (10.15.6). When I run this script from the terminal, it works fine
from selenium import webdriver
But starting it from the Python IDLE or VSCode, I get the message:
ModuleNotFoundError: No module named 'selenium'
I have no clue why it works from the terminal and not from the shell. It would be great if someone could help me out or point me in a direction where I might find the answer :)
It's seems like the Python interpreter you're using in the shell is not the same one used by Vscode. You can change the Python environment to match the one you're using in your Shell, or even install selenium in the Python interpreter currently used with:
python -m pip install -U selenium
Try typing which python in terminal and console, it will point out which python it is using. You can set the python again in VSCode using ⇧⌘P, Select interpreter then choose the one that's working in terminal.

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

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.

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!

how do i select an interpreter on my IDE which is Python IDLE

How do I select an interpreter on my IDE which is Python IDLE? I can't find the options to do that.
I managed to install Tensorflow but it only works when I import it in the terminal, not in my current IDE
What I want: Make my current IDE use the Python.exe that has been provided when I installed Tensorflow on my computer
What I tried: Using PYCHARM, it works (like a charm!) but I can't do that stuff like import module then have " >>> " then issue my commands etc...
IDLE does NOT provide such functionality - it works through idlelib, a package from stdlib so it's executed using pythonw -m idlelib. To change interprenter in IDLE, call it using a different interprenter - "C:\path\to\your\python\interprenter\pythonw.exe" -m idlelib (make sure idlelib is installed for target interprenter).

Categories

Resources