Python: Cannot import from custom module - python

I am having an issue with python not being able to find or import my module. I am unsure how to fix this problem. My py file I'm trying to get my import into is in 'main' folder, the file I'm trying to import is one level up. My program is a package:
Error I'm getting
models.py

Trying copying your .py file to site-packages folder present in the Python folder and then running your python file from command prompt(python -m <your_file_name>.py)/python launcher/IDE. In case you are still getting the error then share your code as well so that people can see.

Related

Python file not getting imported from different directory in robot framework

Hi I am trying to import python file in my robot code using library function like this.
Library /Users/test/Desktop/bb/src/json.py
Library /Users/test/Desktop/bb/src/csv.py
I have another python file in same directory that is working. I imported like this.
Library ./API.py
But those two files not getting imported. I am using Pycharm and mac os. I tried setting python path changing interpreter. nothing works for me.
Python path in pycharm
["/Users/test/Desktop/bb/src/"]
Any help will be much appreciated.
I'm not familiar with the 'Library' command, but if it follows the same logic as 'import', then the following can help:
Let's say I have a python file 'module_test.py' located in 'C:/Users/Public/'
, and I'm coding in a different directory. Then to import this file, I should add it to the path list before importing:
import sys
sys.path.insert(0, 'C:/Users/Public/')
import module_test
a = module_test.example_function()
the argument '0' means it's being added as the first item of the path list.
But I see that this robot framework might not work as straightforward as that. This answer here might help: Import custom library from a different path in Robot Framework
Well for most of the time your python script fails to import in your robot script is mainly because of 2 reasons-
1. Python Script is having errors.
2. Specified python file's path under *** settings *** tag is not correct.
So make sure that python script is executing successfully without any errors and path for python file is correct that you're importing.

ModuleNotFoundError when working with multiple folders

I got a question/problem related to how to use init.py file for a project im working on. Im trying to run train_pipeline.py, which is located at
packages/cnn_modality_clf/cnn_modality_clf/train_pipeline.py
This file uses scripts that are located in the same folder, and also files that are within folders.
My imports (within train_pipeline.py
from cnn_modality_clf.pipeline import pipe
from cnn_modality_clf.config import config
from cnn_modality_clf.preprocessing import data_management as dm
from cnn_modality_clf.preprocessing import preprocessors as pp
However, when I run this train_pipeline file, I get a ModuleNotFoundError: No module named cnn_modality_clf and the error is on from cnn_modality_clf.pipeline import pipe
I have worked on previous projects (not that many) where using the init.py file will solved this issue. However, I have added this file into every folder. But that doesnt seem to work out.
My project structure looks like this:
I want to be able to use the python files that are within the same path, or that are within a folder (cnn.modality_clf.pipeline import pipe)
*Im using anaconda environment, and im using python 3.7.0
Any suggestions on how to overcome this problem?
Thanks in advance!

Need to fix problem import local file in python

i can't fix this problem, [ModuleNotFoundError: No module named 'folder02' ] python3, using virtual environment (anaconda) - vscode
i cant import local file
from folder02.file02 import file02
You are running a file inside your project. The file doesn’t know about it’s neighbouring files in the rest of the project. You need to run python with the ‘-m’ flag.
For example if the file you want to run is inside ‘folder1/file1.py’
Run with ‘python -m folder1.file1’

how to perform explicit relative import in python2?

My team mate added a explicit relative import to the code base.
from .modulename import classname
Now if I try to run that file, I get the "Attempted relative import in non-package" error?
I am trying to run this in Python2, he put in this change to make this file compatible with Python3. For the time being, I have to run this file in Python2
The file that I am trying to run and the file that it is trying to import is in the same directory which is the directory from where I am trying to launch this file.
Explicit relative imports work exactly the same in Python 2 and Python 3. The problem is that one of you is running your code wrong - probably you, although there isn't enough information here to be certain.
If this file is supposed to be part of a package, then you're the one running it wrong. You should not be running it directly by filename, and you should not be inside the package directory when you run it. In fact, there's a good chance you shouldn't be running it at all, only importing it from other files. If you do want to run it, the command would be python -m packagename.modulename from somewhere where the top-level package is importable - probably the directory containing the package's directory, if the package isn't installed.
If this file is not supposed to be part of a package, then your teammate is running it wrong. The explicit relative import should be converted to an absolute import, and you should add
from __future__ import absolute_import
to the top of the file to ensure you don't get any accidental implicit relative imports. In this case, your partner will need to stop running the file however they've been running it.

Python can't find module in the same folder

My python somehow can't find any modules in the same directory.
What am I doing wrong? (python2.7)
So I have one directory '2014_07_13_test', with two files in it:
test.py
hello.py
where hello.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
def hello1():
print 'HelloWorld!'
and test.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
from hello import hello1
hello1()
Still python gives me
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 4, in <module>
ImportError: No module named hello
What's wrong?
Change your import in test.py to:
from .hello import hello1
Your code is fine, I suspect your problem is how you are launching it.
You need to launch python from your '2014_07_13_test' directory.
Open up a command prompt and 'cd' into your '2014_07_13_test' directory.
For instance:
$ cd /path/to/2014_07_13_test
$ python test.py
If you cannot 'cd' into the directory like this you can add it to sys.path
In test.py:
import sys, os
sys.path.append('/path/to/2014_07_13_test')
Or set/edit the PYTHONPATH
And all should be well...
...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'
There is a better shebang you should use.
Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.
I had a similar problem, I solved it by explicitly adding the file's directory to the path list:
import os
import sys
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)
After that, I had no problem importing from the same directory.
Here is the generic solution I use. It solves the problem for importing from modules in the same folder:
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
Put this at top of the module which gives the error "No module named xxxx"
In my case, Python was unable to find it because I'd put the code inside a module with hyphens, e.g. my-module. When I changed it to my_module it worked.
I ran into this issue. I had three folders in the same directory so I had to specify which folder.
Ex: from Folder import script
I had somewhat of a similar problem. I could not import modules even though they all were in the same directory (importError). I tried out the solutions above but none of them worked for me. I had to set up the path myself (manually). Also, the code was run on my university server, perhaps that's why I had to set the path manually.
import sys
sys.path.append(r'path_to_directory_where_all_modules_are')
I recommend reading The Module Search Path
The following doesn't solve the OP's problem, but the title and error is exactly what I faced.
If your project has a setup.py script in it, you can install that package you are in, with python3 -m pip install -e . or python3 setup.py install or python3 setup.py develop, and this package will be installed, but still editable (so changes to the code will be seen when importing the package). If it doesn't have a setup.py, make sense of it.
Anyway, the problem OP faces seems to not exist anymore?
file one.py:
def function():
print("output")
file two.py:
#!/usr/bin/env python3
import one
one.function()
chmod +x two.py # To allow execution of the python file
./two.py # Only works if you have a python shebang
Command line output: output
Other solutions seem 'dirty'
In the case of OP with 2 test files, modifying them to work is probably fine. However, in other real scenarios, the methods listed in the other answers is probably not recommended. They require you to modify the python code or restrict your flexibility (running the python file from a specific directory) and generally introduce annoyances. What if you've just cloned a project, and this happens? It probably already works for other people, and making code changes is unnecessary. The chosen answer also wants people to run a script from a specific folder to make it work. This can be a source of long term annoyance, which is never good. It also suggests adding your specific python folder to PATH (can be done through python or command line). Again, what happens if you rename or move the folder in a few months? You have to hunt down this page again, and eventually discover you need to set the path (and that you did exactly this a few months ago), and that you simply need to update a path (sure you could use sys.path and programmatically set it, but this can be flaky still). Many sources of great annoyance.
If you are sure that all the modules, files you're trying to import are in the same folder and they should be picked directly just by giving the name and not the reference path then your editor or terminal should have opened the main folder where all the files/modules are present.
Either, try running from Terminal, make sure first you go to the correct directory.
cd path to the root folder where all the modules are
python script.py
Or if running [F5] from the editor i.e VsCode then open the complete folder there and not the individual files.
After spending hours to get imports working like:
from business import Business
from .business import Business
import .Business
import business.Business
...
I got finally rid of my embedded python installation and installed python from the scratch by be the .exe file for all users like in
c:\Program Files\Python310
then I made sure my PATH Variable is up to date with the new installation (so what we want to see or make are entries like c:\Program Files\Python310 and c:\Program Files\Python310\Scripts and %USERPROFILE%\AppData\Roaming\Python\Python310\Scripts) and started a cmd with administrator privileges, downloaded the get-pip.py file and run it in the elivated cmd like python get-pip.py and finaly everything worked as expected... I don't know why or what I did wrong or so, but python really seems that it need to be integrated deeply into windows or it just do not work the easy way. It doesn't happen too often, but in this case it worked a lot better in linux ;)
Also recheck spelling of both the file and the module for typos.
For example
import passwords
When the file name has been saved as password missing an s.
It might sound obvious but it can sometimes be something as simple as this when all other advice above not working :)
This kind of problems happens when your project path is changed. You need to use:
cd path\to\the\files_path
simply
The same error, but I didn't find an answer to my case, maybe someone need my solution.
It appears that in PyCharm I've created .py file, but somehow in windows directory of my project file was blank and without .py. So I rename extension right in derictory and it worked.
Picture before changing an extension

Categories

Resources