As a beginner with Python I've been following the Automate the Boring Stuff with Python book and I'm trying to get the ezgmail module to work by importing it in IDLE. I've already succesfully installed ezgmail via the 'pip install ezgmail' command in command prompt but when I try to import ezgmail in IDLE I get a module not found error:
import ezgmail
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
import ezgmail
ModuleNotFoundError: No module named 'ezgmail'
I've used pip to install other modules that I could succesfully import in IDLE so I don't understand what the problem is here. I know this is pretty basic stuff but its frustrating not having the experience to solve it myself. I appreciate any help.
You probably have two different python interpreters. Could you confirm the version you're using currently with python3 --version. Another thing you can do is to always create a virtual environment with virtualenv envname and then activating the environment using source envname/bin/activate before making installation of the libraries.
OR
Another alternative is to just clone the repo of ezgmail from Github. Here is the link: https://github.com/asweigart/ezgmail
Once you've cloned it, you can create a project folder and place ezgmail in the folder. Then import it using:
from ezgmail.src import ezgmail
This is my current folder structure:
|--testing
|-- demo.py
|-- ezgmail
so for me I'm importing ezgmail inside demo.py
NB: you will need to install google-api-python-client and oauth2client
https://stackoverflow.com/a/50964114/16498710
This is what finally worked for me:
"Open python in cmd (type python and press enter)
Import the module in cmd (type import modulename)
Type modulename.file
You will get the path where the module is stored
Copy the corresponding folder
In IDLE, import sys and typing sys.executable to get the paths where it looks for modules to import
Paste your module's folder in the path where IDLE looks for modules.
This method worked for me."
Related
i'm trying execute project python in terminal but appear this error:
(base) hopu#docker-manager1:~/bentoml-airquality$ python src/main.py
Traceback (most recent call last):
File "src/main.py", line 7, in <module>
from src import VERSION, SERVICE, DOCKER_IMAGE_NAME
ModuleNotFoundError: No module named 'src'
The project hierarchy is as follows:
Project hierarchy
If I execute project with any IDE, it works well.
Your PYTHONPATH is determined by the directory your python executable is located, not from where you're executing it. For this reason, you should be able to import the files directly, and not from source. You're trying to import from /src, but your path is already in there. Maybe something like this might work:
from . import VERSION, SERVICE, DOCKER_IMAGE_NAME
The interpretor is right. For from src import VERSION, SERVICE, DOCKER_IMAGE_NAME to be valid, src has to be a module or package accessible from the Python path. The problem is that the python program looks in the current directory to search for the modules or packages to run, but the current directory is not added to the Python path. So it does find the src/main.py module, but inside the interpretor it cannot find the src package.
What can be done?
add the directory containing src to the Python path
On a Unix-like system, it can be done simply with:
PYTHONPATH=".:$PYTHONPATH" python src/main.py
start the module as a package element:
python -m src.main
That second way has an additional gift: you can then use the Pythonic from . import ....
I have the following files:
projx
proj1
__init__.py
mod1.py
tests
test_mod1.py
The package proj1 is already installed on my development machine using pip install. And test_mod1.py has the following import
from proj1.mod1 import ....
However, when running python proj1\tests\test_mod1.py in directory projx, it still import the package from the old package of proj1.mod1 installed by pip install instead of the new one from .\proj1\mod1.py. Shouldn't python use the later one because the current directory is the first item in sys.path?
I created and active venv. However, it got the following error when executing the test file.
Traceback (most recent call last):
File ".\tests\test_......py", line 4, in <module>
from proj1.mod1 import *
ModuleNotFoundError: No module named 'proj1'
I tried to use relative path.
from ..proj1 import *
But It got error of:
ImportError: attempted relative import with no known parent package
You can install multiple version of the same package! Using: pip install packageName will download the newest version, using: pip install packageName==1.0.0 will let you choose older versions aswell.
To import the newest: import packageName.
To import a specified: import packageName_1.0.0
However I think you are right, CWD should always be priority.
I think if you were to try this: from .. import proj1, because parent directories will not be searched for packages.
To demonstrate:
me#myPc /parentDir % ls
childDir
me#myPc /parentDir % cat childDir/cwd.py
import os
print(os.getcwd())
me#myPc /parentDir % python3 childDir/cwd.py
/parentDir
Even though your CWD when telling python to execute the script was projx, the CWD of your script will be it's location: tests. So using from .. import proj1 would tell your script to look for proj1 in it's parent directory and import it.
I am getting this error
Traceback (most recent call last):
File "Exporter.py", line 3, in <module>
import sys,getopt,got,datetime,codecs
File "C:\Users\Rohil\Desktop\GetOldTweets-python-master\got\__init__.py", line 1, in <module>
import models
ModuleNotFoundError: No module named 'models'
my directory tree is:
C:\Users\Rohil\Desktop\GetOldTweets-python-master\got
this contains 2 folders: manager and models and 1 __init__.py file with the code :
import models
import manager
i am executing a file with the path: C:\Users\Rohil\Desktop\GetOldTweets-python-master\Exporter.py
I can't figure out what the issue is. can anyone assist me?
Set the environment variable PYTHONPATH=C:\Users\Rohil\Desktop\GetOldTweets-python-master\got (how exactly, depends on your operating system)
If you have created a directory and sub-directory then follow the below steps and please keep in mind that a directory must have an __init__.py file for python to recognize it as a package.
First run this to see all paths being searched by python:
import sys
sys.path
You must be able to see your current working directory in that list.
Now import the sub-directory and the respective module that you want to use via the import command: import subdir.subdir.modulename as abc You should now be able to use the methods in that module.
As you can see in this screenshot above I have one parent directory and two sub-directories. Under the second sub-directory I have a module named CommonFunction. On the console to the right you can see my working directory after execution of sys.path.
Does the models folder has an __init__.py file inside it ? Only then, it will be recognized as a module by python and import models would make sense.
So,
Create an empty __init__.py file in the models subfolder and then the code should work without any issues.
You should also look at this answer.
if create d or using or custom python package
check our dir. correct.
** For python 3.7 user **
from. import module_name
If you are using python3 but are installing the API through 'pip install 'such and such'. It's not gonna work on python3 console. Therefore, you'd better use 'sudo python3 -m pip install 'such and such''. Then it's gonna work!
(at least for ubuntu stuff)
:)
i'm trying to learn python pyramid for linux and following the pylonsproject documentation in doing so.I'm also new to linux.
I've installed everything correctly(I'm pretty sure), but when i invoke my helloworld.py, i got this following error
ImportError: No module named pyramid.config
the documentation says this should be the path to the file
$ /path/to/your/virtualenv/bin/python helloworld.py
but i'm confused as the python after the bin is a executable not a directory? my env is located in the Downloads folder
Thanks!
These symptoms are almost always a misuse of the virtualenv in one way or another.
1) Make sure you are running the virtualenv
$ env/bin/python helloworld.py
2) Make sure you installed pyramid into the virtualenv
$ env/bin/python
>>> import pyramid.config
# ImportError or not?
I'm having a hard time understanding how module importing works in Python (I've never done it in any other language before either).
Let's say I have:
myapp/__init__.py
myapp/myapp/myapp.py
myapp/myapp/SomeObject.py
myapp/tests/TestCase.py
Now I'm trying to get something like this:
myapp.py
===================
from myapp import SomeObject
# stuff ...
TestCase.py
===================
from myapp import SomeObject
# some tests on SomeObject
However, I'm definitely doing something wrong as Python can't see that myapp is a module:
ImportError: No module named myapp
In your particular case it looks like you're trying to import SomeObject from the myapp.py and TestCase.py scripts. From myapp.py, do
import SomeObject
since it is in the same folder. For TestCase.py, do
from ..myapp import SomeObject
However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py, you would have to mess with your path. This can be done within Python:
import sys
sys.path.append("..")
from myapp import SomeObject
though that is generally not recommended.
In general, if you want other people to use your Python package, you should use distutils to create a setup script. That way, anyone can install your package easily using a command like python setup.py install and it will be available everywhere on their machine. If you're serious about the package, you could even add it to the Python Package Index, PyPI.
The function import looks for files into your PYTHONPATH env. variable and your local directory. So you can either put all your files in the same directory, or export the path typing into a terminal::
export PYTHONPATH="$PYTHONPATH:/path_to_myapp/myapp/myapp/"
exporting path is a good way. Another way is to add a .pth to your site-packages location.
On my mac my python keeps site-packages in /Library/Python shown below
/Library/Python/2.7/site-packages
I created a file called awesome.pth at /Library/Python/2.7/site-packages/awesome.pth and in the file put the following path that references my awesome modules
/opt/awesome/custom_python_modules
You can try
from myapp.myapp import SomeObject
because your project name is the same as the myapp.py which makes it search the project document first
You need to have
__init__.py
in all the folders that have code you need to interact with.
You also need to specify the top folder name of your project in every import even if the file you tried to import is at the same level.
In your first myapp directory ,u can add a setup.py file and add two python code in setup.py
from setuptools import setup
setup(name='myapp')
in your first myapp directory in commandline , use pip install -e . to install the package
pip install on Windows 10 defaults to installing in 'Program Files/PythonXX/Lib/site-packages' which is a directory that requires administrative privileges. So I fixed my issue by running pip install as Administrator (you have to open command prompt as administrator even if you are logged in with an admin account). Also, it is safer to call pip from python.
e.g.
python -m pip install <package-name>
instead of
pip install <package-name>
In my case it was Windows vs Python surprise, despite Windows filenames are not case sensitive, Python import is. So if you have Stuff.py file you need to import this name as-is.
let's say i write a module
import os
my_home_dir=os.environ['HOME'] // in windows 'HOMEPATH'
file_abs_path=os.path.join(my_home_dir,"my_module.py")
with open(file_abs_path,"w") as f:
f.write("print('I am loaded successfully')")
import importlib
importlib.util.find_spec('my_module') ==> cannot find
we have to tell python where to look for the module. we have to add our path to the sys.path
import sys
sys.path.append(file_abs_path)
now importlib.util.find_spec('my_module') returns:
ModuleSpec(name='my_module', loader=<_frozen_importlib_external.SourceFileLoader object at 0x7fa40143e8e0>, origin='/Users/name/my_module.py')
we created our module, we informed python its path, now we should be able to import it
import my_module
//I am loaded successfully
This worked for me:
from .myapp import SomeObject
The . signifies that it will search any local modules from the parent module.
Short Answer:
python -m ParentPackage.Submodule
Executing the required file via module flag worked for me. Lets say we got a typical directory structure as below:
my_project:
| Core
->myScript.py
| Utils
->helpers.py
configs.py
Now if you want to run a file inside a directory, that has imports from other modules, all you need to do is like below:
python -m Core.myscript
PS: You gotta use dot notation to refer the submodules(Files/scripts you want to execute). Also I used python3.9+. So I didnt require neither any init.py nor any sys path append statements.
Hope that helps! Happy Coding!
If you use Anaconda you can do:
conda develop /Path/To/Your/Modules
from the Shell and it will write your path into a conda.pth file into the standard directory for 3rd party modules (site-packages in my case).
If you are using the IPython Console, make sure your IDE (e.g., spyder) is pointing to the right working directory (i.e., your project folder)
Besides the suggested solutions like the accepted answer, I had the same problem in Pycharm, and I didn't want to modify imports like the relative addressing suggested above.
I finally found out that if I mark my src/ (root directory of my python codes) as the source in Interpreter settings, the issue will be resolved.