package imported from pip installed one instead of local file? - python

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.

Related

Fixing ModuleNotFoundError for ezgmail

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."

python module not found error no module named

I have a few seperate pythone file and I am using them to import another py file. Modules that trying to import them are in seperate folder I code sample is below
from tez.library.image_crop import ImageCrop
from tez.library.image_process import ImageProcess
from tez.library.image_features import ImageFeatures
from tez.const.application_const import ApplicationConst
from tez.library.file_operation import FileOperation
this code is in where I want to start the py file using commond line as "python samples1.py" and thrown an error as below
Traceback (most recent call last): File "samples1.py", line 1, in
from tez.library.image_crop import ImageCrop ModuleNotFoundError: No module named 'tez'
folder structure :
.tez
-- library
---- image_crop.py
---- image_process.py
---- image_features.py
--src
---- samples1.py
Python version : 3.8
Pip : 20.0.2
Windows 10 Pro 1909
If you are building a package called tez (and since you tried to import it I think you are). Then everything with tez needs to refer to itself locally. All the files in the tez package need to refer to each other with the "." and ".." imports.
In samples1.py:
from ..library.image_crop import <something>
EDIT:
It sounds like you are misunderstanding how python imports things. When you run "import X" in a python script, then python looks for a package named X under sys.path. You can append to sys.path at the top of your script if you have a custom package to look for.
import sys
sys.path.append(<directory of tez>)
import tez
However, it is strongly recommended that you should not be importing from a file that is under the directory structure of the package name. If "examples" is a directory of examples that use the package "tez" then "examples" should be located outside the package "tez". If "examples" is inside the package "tez", then "examples" should be doing local imports "with-in" the package.
Getting a handle on package use can be tricky.
sample.py can't see above of src folder, but you can tell Python to do this.:
import sys
import os
tez = os.path.dirname(os.path.dirname(__file__))
# __file__ is path of our file (samples.py)
# dirname of __file__ is "src" in our state
# dirname of "src" is "tez" in our state
sys.path.append(tez) # append tez to sys.path, python will look at here when you try import something
import library.image_crop # dont write "tez"
But this is not a very good design I think.

Unable to Import a file from Parent Directory

My file structure is as follows:
monitor/
core/
database.py
processor.py
timekeeper.py
jobs/
jobA.py
jobB.py
setup.py
From jobA.py I import like this:
from core.database import Database
from core.timekeeper import Timekeeper
from core.processor import Processor
While at database.py, processor.py and timekeeper.py I import setup.py.
Get the following error when I run jobA.py:
root#test:/var/www/python/monitor# python3 jobs/jobA.py
Traceback (most recent call last):
File "jobs/jobA.py", line 2, in <module>
from core.database import Database
ModuleNotFoundError: No module named 'core'
To allow import core or import core.database (without the relative dots or double-dots) the parent directory of core should either be the current directory, or be included on sys.path. You appear to have a setup.py. Conventionally that means a file that performs installation and packaging tasks via the setuptools or distutils packages. If that is indeed the role it performs, perhaps you need to run it. One way to run it would be to issue (from the command-line outside Python) the command pip install -e /path/to/monitor. Assuming setup.py was written correctly, this will ensure that the core package, in its current location, is lastingly made available for the default Python distribution. Next time you launch Python, /path/to/monitor will be on sys.path and import core will work from (almost) anywhere.
Add
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
to the top of your jobA.py file. If you are using python 3.3+, you do not need an __init__.py file. It needs to be above your other import statements.
From this answer you can use 2 dots to import from a directory above. So you could potentially use:
from .core.database import Database
from .core.timekeeper import Timekeeper
from .core.processor import Processor
Python 3.3+ you don’t need an __init__.py file so I don’t believe just adding one will help.
What module are you trying to use? Maybe your module is not compatible with Python 3.

Python error ' ModuleNotFoundError:'

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)
:)

How to import a module from a directory on level above the current script

For my Python application, I have the following directories structure:
\myapp
\myapp\utils\
\myapp\utils\GChartWrapper\
\myapp\model\
\myapp\view\
\myapp\controller\
One of my class in \myapp\view\ must import a class called GChartWrapper. However, I am getting an import error...
myview.py
from myapp.utils.GChartWrapper import *
Here is the error:
<type 'exceptions.ImportError'>: No module named GChartWrapper.GChart
args = ('No module named GChartWrapper.GChart',)
message = 'No module named GChartWrapper.GChart'
What am I doing wrong? I really have a hard time to import modules/classes in Python...
The __init__.py file of the GChartWrapper package expects the GChartWrapper package on PYTHONPATH. You can tell by the first line:
from GChartWrapper.GChart import *
Is it necessary to have the GChartWrapper included package in your package directory structure?
If so, then one thing you could do is adding the path where the package resides to sys.path at run time. I take it myview.py is in the myapp\view directory? Then you could do this before importing GChartWrapper:
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'utils')))
If it is not necessary to have it in your directory structure, it could be easier to have it installed at the conventional location. You can do that by running the setup.py script that's included in the GChartWrapper source distribution.
You don't import modules and packages from arbritary paths. Instead, in python you use packages and absolute imports. That'll avoid all future problems.
Example:
create the following files:
MyApp\myapp\__init__.py
MyApp\myapp\utils\__init__.py
MyApp\myapp\utils\charts.py
MyApp\myapp\model\__init__.py
MyApp\myapp\view\__init__.py
MyApp\myapp\controller\__init__.py
MyApp\run.py
MyApp\setup.py
MyApp\README
The files should be empty except for those:
MyApp\myapp\utils\charts.py:
class GChartWrapper(object):
def __init__(self):
print "DEBUG: An instance of GChartWrapper is being created!"
MyApp\myapp\view\__init__.py:
from myapp.utils.charts import GChartWrapper
def start():
c = GChartWrapper() # creating instance of the class
MyApp\run.py:
from myapp.view import start
start()
That's all! When you run your entry point (run.py) it calls a function on the view, and that creates an instance of the GChartWrapper class. Using this structure you can import anything anywhere and use it.
To complement, in MyApp\setup.py you write an installation program for the MyApp\myapp package. Use distutils to write it:
from distutils.core import setup
setup(name='MyApp',
version='1.0',
description='My Beautiful Application',
author='Martin',
author_email='martin#xxxxxxx.com',
url='http://stackoverflow.com/questions/1003843/',
packages=['myapp'],
scripts=['run.py']
)
That is enough. Now when people download the MyApp folder, they can just install it using setup.py and run it using run.py. Distutils can generate packages in a number of formats including windows installable .EXE
It's the standard way of distributing python packages/applications.
You can change the path where python looks for files.
At the top of your source file, add:
import sys
sys.path.append("..")
Or alternatively change the environment variable:
export PYTHONPATH=..
Or starting in python 2.5 (again assuming myview is in myapp\view:
from __future__ import absolute_import
from ..utils.GChartWrapper import *
See: http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports
GChartWrapper is also available from PyPI so you can use easy_install or pip to install the module:
sudo pip install GChartWrapper==0.9
It will then be automatically added to your PYTHONPATH and then you can remove it from your /myapp/utils directory. If you can't use sudo, look at using virtualenv (and virtualenvwrapper).

Categories