How to import module from other directory in python - python

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.

Related

Can not import class from another folder

I have a similar issue as this Can't get Python to import from a different folder The solution there doesn't solve my issue.
I'm working with Airflow lib. The lib updated one of the operators and since I can not at this time upgrade the my Airflow version I want to manually download the operator .py file and use it in my code manualy.
airflow
-dags
--mydag.py
-AddedOperators
--sagemaker_tuning_operator.py
--__init__.py (empty file)
The sagemaker_tuning_operator.py is this file:
https://github.com/apache/airflow/blob/master/airflow/contrib/operators/sagemaker_tuning_operator.py
It contains the class SageMakerTuningOperator
In my mydag.py I do:
from AddedOperators.sagemaker_tuning_operator import SageMakerTuningOperator
When Airflow try to parse mydag.py I get:
No module named AddedOperators.sagemaker_tuning_operator
Check if your project directory is in your system path or not. You can can check it as follows:
import sys
print(sys.path)
Note that when running a Python script, sys.path doesn’t care what
your current “working directory” is. It only cares about the path to
the script. For example, if my shell is currently at the Airflow/
folder and I run python ./dags/mydag.py, then sys.path includes
Airflow/dags/ but NOT Airflow/
If you project directory is not in sys path, you can do following:
Include it dynamically.
import sys
sys.path.insert(0, 'path/to/your/')
# import your module now
Import all required modules in your project root folder in some file like app.py and then call from here.

Import Module in sys.path is not found

I have a module that I use in a number of scripts. Its path is C:\PYTHONprojects\utilities\utility_module.py There is an (empty) file called __init__.py in the same folder.
If I import the module inside the PyCharm environment, it works fine:
import utility_module as um but if do the same from the Python command line, I get:
ModuleNotFoundError: No module named 'utility_module'
But the folder is in the PATH:
From the windows command line:
In: PATH
Out:
......
C:\PYTHONprojects\utilities; C:\PYTHONprojects
.....
or from the Python Command Line:
In: import sys
sys.path
Out:
......
'C:\\PYTHONprojects', 'C:\\PYTHONprojects\\utilities'
In order to load your module, it needs to be part of your PYTHONPATH system env (not PATH) or sys.path.
Your pycharm loads the working directory into your path for you. When you do that on command line, you need to take care for that.
So either add your project folder to system env PYTHONPATH or before you load the module, execute this:
import sys
sys.path.append(r'C:\PYTHONprojects\utilities')

Import collection of modules in python

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.

Path appended but python does not find module

I have the following structure:
~/git/
~/git/folder1
~/git/folder2
in ~/git/folder1 I have main.py, which imports doing the following:
import folder2.future_data as future_data
which throws the following error:
import folder2.future_data as f_d
ImportError: No module named folder2.future_data
Despite my $PATH containing
user#mac-upload:~$ echo $PATH
/home/user/anaconda2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/user/git/folder2
Why am I unable to import from folder2 despite it being in my path?
Am I missing something?
Try putting an empty __init__.py file in each directory (~/git, ~/git/folder1, and ~/git/folder2). Then do export PYTHONPATH=${HOME}/git:$PYTHONPATH (assuming bash shell).
This will also allow you to just set your PYTHONPATH once at the top level and be done with it. If you add more directories (modules) that you need to import, you can just keep adding __init__.py files to your structure (instead of having to constantly modify your PYTHONPATH every time your file/directory structure changes).
You can explicitly added the path inside the main.py script before you doing import
import sys
sys.path.append(r'~/git/folder2')
import future_data

Path exists in sys.path but still gives module not found error in Python

So, I print the sys.path in the python terminal inside the working python project directory, and find the directory there. I've also put in a __init__.py in the directory, too.
But when I do a import directoryname, I get a module not found error.
Where can I possibly be going wrong?
Ok, I found the problem. It is giving a no such directory error when I check the $PYTHONPATH.
This is my PythonPath
export PYTHONPATH=/usr/lib/python2.7/dist-packages:/home/python/softwares/orade
I assume that you are trying to import the orade module. PYTHONPATH represents the path to the directory that contains python modules, not the collection of paths to the modules.
So you should put the parent directory of your module in the PYTHONPATH. If the path to your module is /home/python/softwares/orade, you should put /home/python/softwares in your PYTHONPATH:
export PYTHONPATH=/usr/lib/python2.7/dist-packages:/home/python/softwares
And then you should be able to do:
>>> import orade
If the orade directory contains a __init__.py file.

Categories

Resources