Import Module in sys.path is not found - python

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

Related

How to import module from other directory in 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.

python vscode modulenotfounderror

I have a certain project structure:
- azima
- .vscode
- core
- project_setup.py
- helper
- log_helper
- venv
In project_setup.py:
import os
import json
import numpy as np
import pandas as pd
import random
from helper.log_helper import log
if __name__ == "__main__":
print('hello world')
Running this file in terminal:
(venv) rmali#rakeshmali:~/git/azima$ /home/rmali/git/azima/venv/bin/python /home/rmali/git/azima/core/project_setup.py
Traceback (most recent call last):
File "/home/rmali/git/azima/core/project_setup.py", line 6, in <module>
from helper.log_helper import log
ModuleNotFoundError: No module named 'helper'
I get this error. What am I doing wrong? Am I missing something?
But running like this python -m core.project_setup works.
Reason:
The path of folder azima does not in the sys.path(PYTHONPATH).
Solution:
You can do this to modify the PYTHONPATH:
Add these in the settings.json file to Modify the PYTHONPATH in the terminal:
"terminal.integrated.env.windows": {
"PYTHONPATH": "xxx/site-packages"
}
Create a .env file under your workspace, and add these settings in it to modify the PYTHONPATH for the extension and debugger:
PYTHONPATH=xxx/site-packages
You can refer to here to understand the effects of these two configurations.
Modify it directly in the python file. Add these codes in the b.py file.
import sys; sys.path.append("xxx/Project/src")
The reason that running
(venv) rmali#rakeshmali:~/git/azima$ python ./core/project_setup.py
fails while
(venv) rmali#rakeshmali:~/git/azima$ python -m core.project_setup
succeeds is that when running python -m <module-name, Python adds the current directory to the start of sys.path, which allows modules in that directory such as helper to be imported as top level modules, i.e. with import helper. Running python <script> does not add the current directory to the start of sys.path. Instead, Python adds the directory containing the script to the start of sys.path.
Here are the relevant sections of the docs.
The -m switch
As with the -c option, the current directory will be added to the start of sys.path.
and the docs for the -c option add
the current directory will be added to the start of sys.path (allowing modules in that directory to be imported as top level modules)
Docs for python <script>
If the script name refers directly to a Python file, the directory containing that file is added to the start of sys.path, and the file is executed as the __main__ module.
If the script name refers to a directory or zipfile, the script name is added to the start of sys.path and the __main__.py file in that location is executed as the __main__ module.

Python ModuleNotFoundError when not in working directory; getcwd finds module

When I try to run a python file with python3.6 src/main.py (the working directory is one above src) there's this error when importing another module from path src:
from src import another_module
ModuleNotFoundError: No module named 'src'
When I do
print(os.getcwd())
print(os.listdir(os.getcwd()))
I get what's expected:
path/to/working/directory
['src']
The import works when I run the script with PyCharm, but I need to run it outside PyCharm.
To make src a package, make an empty file named src/__init__.py
When you run a python script on the command line, the script's directory (which may not be the same as your shell current working directory) is added to the path.
So, since src/ is already in your path, you can just say import another_module.
Solved by creating another file run.py in the working directory that calls src/main.py and running run.py from the command line instead of src/main.py.
In launch.json, add the following line to your deployment:
"env": {"PYTHONPATH": "${workspaceRoot}"}
This seems to force Python to evaluate imports relative to the ${workingSpaceRoot} folder; allowing you to use a fully qualified name: from src import ...
The default behavior is to evaluate namespaces relatively. So, your original statement: from src import ... is actually looking in src/src/....
(Credit to #g4th who posted this solution on another StackOverflow page.)

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.

python, import file under current folder from a file under other directory

I have a package which intends to import a file that user (me) provides
In c:\lib\calc.py
# some codes to find which file to import
filename = "A"
__import__(filename)
...
And I have a file c:\scripts\A.py
Note A.py is in a different folder than calc.py. Now I'm supposed to do this under command window
cd C:\scripts
python c:\lib\calc.py
but I get an error message
No module named A
A.py is in the current folder, why does python fail to find it? How can I make A.py available?
Thanks.
You can add the path to your scripts directory in your script calc.py so it can find the A.py module.
import sys
sys.path.append('C:\scripts')
If you are on Python version 2.7 or newer you can use the importlib package. The import_module function can accept an absolute path which, IIRC, does not need to be on your PYTHONPATH.
import importlib
mod = importlib.import_module(filepath)
Or, if you wanted to manually do the work yourself (or if you are using Python 2.6 or earlier), you can use the imp package directly:
import imp
mod = imp.load_module(imp.find_module(filename, filepath))

Categories

Resources