I trying to import a package that I had created and when i tried to import it,
the following output is shown:
(ModuleNotFoundError: No module named 'folder1')
in each folder in my package (__init__.py) has been created.
the package is in the following path
(/Users/user/Desktop/package/folder1/test.py)
python is in the following path(/Users/user/anaconda3/lib/python3.6'
how can I import the package? do I need to change package's location?
thank you for help
Try adding your package to the python path:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
When you import a python module using import python interpreter searches in current directory & paths set in PYTHONPATH environment variable.
so inorder to work what you are expecting either you should be running the script insider the parent directory or you must include in PYTHONPATH environment variable to you folder path.
Related
I am facing a problem in importing modules in python. I looked for a solution and found this. but this did not worked either.
My Directory is as follows
->MyScrapper --->MyScrapper ----->db_connection.py --->Video_Scrapper -----> video_scrapper.py --->Blogs_Scrapper -----> blogs_scrapper.py
I want to import db_connection.py in video_scrapper.py as well as blogs_scrapper.py. I have tried to import it as from MyScrapper.db_connection import DBConnection. It throws a ModuleNotFoundError: No Module named 'MyScrapper'. I have also tried using from db_connection import DBConnection and import DBConnection but none of them worked.
Please Help!!
When importing a module in python, it will search the module in this order:
build-in module
script in the current directory
PATHPYTHON
installation-dependent default
So, you need to add the MyScrapper module to the search path. If you use a virtual environment, you can add your project root directory into the PYTHONPATH, by modify the PYTHONPATH in the Scripts\activate.bat file, like this:
set PYTHONPATH=%VIRTUAL_ENV%\src;%PYTHONPATH%
I use the virtual environment, and my project struct is:
|--Include
|--Lib
|--Scripts
|--src
My source files are in the src directory.
I have a file tree like this:
app
|---src
| |---vlep/config.py
|
|---tests
|---conftest.py
from conftest.py I am trying to import vlep.confi as config and I am getting the ModulenotFoundError: No module named 'vlep'. I am using venv and I added absolute paths for app, src and vlep directories to venv/bin/activate, so when I activate the virtualenv I can have these directories in the PYTHONPATH env. I thought that this would do the job, but no...and have no idea why. What am I doing wrong?
You can insert the path to the src folder in sys.path like this:
import os
import sys
sys.path.insert(0, f'{os.path.dirname(os.path.abspath(__file__))}/../src')
from vlep import config
This way the absolute path to your src directory will be first when Python resolves where to import the module from. And it also does not matter what directory you run conftest.py from.
You need to set the file path manually to import the modules from another directory. You can assign a directory path to the PYTHONPATH variable and still get your program working.
In Linux, you can use the following command in the terminal to set the path:
export PYTHONPATH='path/to/directory'
In Windows system :
SET PYTHONPATH='path/to/directory'
To see if PYTHONPATH variable holds the path of the new folder, you can use the following command:
echo $PYTHONPATH
Then you can do your imports:
from conftest import vlep.confi as config
Tks for your time guys. After all there was not wronging to what I was doing to allow the import. The export PYTHONPATH=":/home/user/app/src/vlep" added to the end of venv/bin/activate was enough. The problem was that I was running a script that was changing the value of PYTHONPATH.
As per screen print, import shows error in Python 3.7 version, earlier it was working fine in version Python 2.7 and I am using IntelliJ Idea.
If you see, EOC related .py files are in the same folder and have classes which are being called in Main_EOC.py by passing objects which are inter-related. It's amazing to see the red line while importing files from same folder.
Please help me why it's showing such error
"This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.`"
Also, if you see the line which have full path, is not showing error
from EOC_Module.eoc.script.config import Config
Please help me if there is a way to add this full path on top of the code or other option.
The behavior of import path search changed between python2 and python3. The import path always includes the directory from which the main module was loaded, but it no longer includes directories from which modules were imported.
You need to change your import statement syntax as follows, if you want to import a module that lives in the same directory as the module in which you do the import:
# old way, import works if the named module is in this module's directory
import x
# new (Python3) way:
from . import x
For the second part: adding a path so all code can import from a certain directory: if that directory is (and will always be) relative to your main: you can add a few lines in the main module to make it available. Something like this:
import sys # if you haven't imported it already
import os.path
home = os.path.dirname(sys.argv[0])
sys.path.append( os.path.join(home, "EOC_Module/eoc/script") )
# now, you can import straight from the script directory
import EOC_Intraction
When using pycharm the root directory for your python executable is the same as the root directory of your project, this means that python will start looking for files in the root directory with this files:
.idea/
EOC_module/
logs/
reports/
sql/
This is the reason of why: from EOC_Module.eoc.script.config import Config works.
If you execute your code from the terminal with: python3 Main_EOC.py (not pycharm) the root directory for your python will be the same as the one containing the file, all the other imports will work but from EOC_Module.eoc.script.config import Config not.
So you need to make your imports from project directory if you are using pycharm.
I'm working on a project where all the code in the source tree is separated into module directories, e.g.:
modules/check/lib/check.py
modules/edit/lib/edit.py
During installation, the Python files are put in the same directory program_name under Python's site-packages. All the modules therefore use the syntax import program_name.edit.
Because of the directory and import structure, the source modules are unable to import each other, so you'd have to install them each time you want to run anything in the source tree.
My questions are therefore: Without modifying the directory structure, how can I make sure that modules/check/lib/check.py imports from modules/edit/lib/edit.py and that site-packages/program_name/check.py imports from site-packages/program_name/edit.py? And for a possible reorganization, what are best practices for the directory structure and imports in an environment like this?
You can just add the /modules/ directories to your PYTHONPATH in your dev environment. Once installed in site-packages, calling import edit inside check.py will import the correct module since they are in the same directory. Calling import edit from your dev environ will import the one you added to your PYTHONPATH
Why don't you install symlinks underneath prog_name on your dev machine?
I'm developing/testing a package in my local directory. I want to import it in the interpreter (v2.5), but sys.path does not include the current directory. Right now I type in sys.path.insert(0,'.'). Is there a better way?
Also,
from . import mypackage
fails with this error:
ValueError: Attempted relative import in non-package
You can use relative imports only from in a module that was in turn imported as part of a package -- your script or interactive interpreter wasn't, so of course from . import (which means "import from the same package I got imported from") doesn't work. import mypackage will be fine once you ensure the parent directory of mypackage is in sys.path (how you managed to get your current directory away from sys.path I don't know -- do you have something strange in site.py, or...?)
To get your current directory back into sys.path there is in fact no better way than putting it there.
Keep it simple:
try:
from . import mymodule # "myapp" case
except:
import mymodule # "__main__" case
See the documentation for sys.path:
http://docs.python.org/library/sys.html#sys.path
To quote:
If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.
So, there's no need to monkey with sys.path if you're starting the python interpreter from the directory containing your module.
Also, to import your package, just do:
import mypackage
Since the directory containing the package is already in sys.path, it should work fine.
If you want to run an unmodified python script so it imports libraries from a specific local directory you can set the PYTHONPATH environment variable - e.g. in bash:
export PYTHONPATH=/home/user/my_libs
python myscript.py
If you just want it to import from the current working directory use the . notation:
export PYTHONPATH=.
python myscript.py
Inside a package if there is setup.py, then better to install it
pip install -e .
A simple way to make it work is to run your script from the parent directory using python's -m flag, e.g. python -m packagename.scriptname. Obviously in this situation you need an __init__.py file to turn your directory into a package.
Using sys.path should include current directory already.
Try:
import .
or:
from . import sth
however it may be not a good practice, so why not just use:
import mypackage
A bit late to the party, but this is what worked for me:
>>> import sys
>>> sys.path.insert(0, '')
Apparently, if there is an empty string, Python knows that it should look in the current directory. I did not have the empty string in sys.path, which caused this error.
Speaking for python3.. I wanted to use an improved version of a library that's installed in my environment. There are some extra print statements it makes to show that it and not the original lib is being used.
I placed the lib's folder next to the python script. Ran the script.. it ran with the local lib with the modifications.
Removed the folder and ran it again - this time it ran with the installed lib.
So, solution was simple : place the lib's folder (with same name as in your import statement) in your project folder. That does the job, at least at my end.
This is on a standard Linux Mint 20.04 system, with a python 3.8 virutal environment activated (so "(py3.8)" appears in my terminal when I'm in the virtual env)
You can import package_name if the package is a module: this needs you have init.py under the package and things that you want to use are needed to import in the init.py
Or if you want to import class under the package, you can use from package_name import class_name