Previously, if I need to import files from parent files
I'll do something like
from .some_module import some_class
Now I'm using the code from Udacity (https://github.com/udacity/artificial-intelligence-for-trading/tree/master/project/project_1), specifically, in the .ipynb, the import code is
import sys
!{sys.executable} -m pip install -r requirements.txt
import pandas as pd
import numpy as np
import helper
import project_helper
import project_tests
But this gives error
Invalid requirement: '../../requirements.txt'
and
File "<ipython-input-4-504bb0e8ad18>", line 3, in <module>
import helper
File "G:\Udacity - Artificial Intelligence AI for Trading v1.0.0\artificial-intelligence-for-trading-master\project\project_1\helper.py", line 1
../../helper.py
And when I look the actual files, they simply refer to files like this:
Is such practice legit? If this is legit, how can I solve the above errors?
You need to create the python file first, then you need to create the functions :
e.g.
def welc(x):
print("welcome ",x)
then save it and name it as you like.
then go to another python file in the same directory as the first one and
write:
import the_first_file_name #without .py
the_first_file_name.welc("Abdx")
#Output : welcome Abdx
Related
For a project I have a script that has to import a module from a different folder.
My file structure is the following:
The code in script.py is the following:
import pandas as pd
import numpy as np
from algorithms.kmeans.kmeans import *
representatives = ["WestVlaanderen", "Hainaut", "Brussels", "BrabantWallon"]
representatives_info = pd.read_csv("data\\filtered_data\KMEANS.csv")
representatives_info = representatives_info[representatives_info["PROVINCE"].isin(representatives)]
representatives_info = representatives_info[["INFECTION_RATE", "HOSPITALISATION_RATE", "TEST_POS_PERCENTAGE"]].to_numpy()
kmeans = Kmeans("data\filtered_data\KMEANS.csv", 4, representatives_info)
kmeans.start_clustering()
Both the folder algorithms and kmeans both have their own init.py file (empty but I read that this doesn't matter). And I still can't import the wanted file.
The error I get is:
No module named 'algorithms'
When I try using the same import on the python interactive mode in my bash terminal it works without errors. I have no idea what I'm doing wrong for the import not to work
I have a file called hotel_helper.py from which I want to import a function called demo1, but I am unable to import it.
My hotel_helper.py file:
def demo1():
print('\n\n trying to import this function ')
My other file:
from hotel.helpers.hotel_helper import demo1
demo1()
but I get:
ImportError: cannot import name 'demo1' from 'hotel.helpers.hotel_helper'
When I import using from hotel.helpers.hotel_helper import * instead of from hotel.helpers.hotel_helper import demo1 it works and the function gets called. I tried importing the whole file with from hotel.helpers import hotel_helper and then call the function with hotel_helper.demo1() and it works fine. I don't understand what's wrong in first method. I want to directly import function rather using * or importing the whole file.
If you filename is hotel_helper.py you have to options how to import demo1:
You can import the whole module hotel_helper as and then call your func:
import hotel_helper as hh
hh.demo1()
You can import only function demo1 from module as:
from hote_helpers import demo1
demo1()
From your fileName import your function
from hotel.helpers import demo1
demo1()
You can import a py file with the following statement:
# Other import
import os
import sys
if './hotel' not in sys.path:
sys.path.insert(0, './hotel')
from hotel import *
NOTE:
For IDE like PyCharm, you can specify the import path using the Project Structure setting tab (CTRL+ALT+S)
Helpful stack overflow questions [maybe off topic]:
What is the right way to create project structure in pycharm?
Manage import with PyCharm documentation:
https://www.jetbrains.com/help/pycharm/configuring-project-structure.html
This is probably a duplicate of: https://stackoverflow.com/posts/57944151/edit
I created two files (defdemo.py and rundefdemo.py) from your posted 2 files and substituted 'defdemo' for 'hotel.helpers.hotel_helper' in the code. My 2 files are in my script directory for Python 3.7 on windows 10 and my script directory is in the python path file python37._pth. It worked.
defdemo.py
def demo1():
print('\n\n trying to import this function ')
rundefdemo.py
from defdemo import demo1
demo1()
output
trying to import this function
I was able to solve the issue, it was related to some imports I was making in my file, when I removed all the import statement in my hotel_helper.py ,the code started working as expected , Still I don't understand reason why the issue was occurring. anyway it works.
This ImportError can also arise when the function being imported is already defined somewhere else in the main script (i.e. calling script) or notebook, or when it is defined in a separate dependency (i.e. another module). This happens most often during development, when the developer forgets to comment out or delete the function definition in the body of a main file or nb after moving it to a module.
Make sure there are no other versions of the function in your development environment and dependencies.
detector_main.py
import ast
import os
import fpdf
from Detector.class_coupling_detector import detect_class_cohesion
from Detector.cyclomatic_complexity_detector import detect_cyclomatic_complexity
from Detector.long_lambda_detector import detect_long_lambda
from Detector.long_list_comp_detector import detect_long_list_comp
from Detector.pylint_output_detector import detect_pylint_output
from Detector.shotgun_surgery_detector import detect_shotgun_surgery
from Detector.useless_exception_detector import detect_useless_exception
from tools.viz_generator import add_viz
import sys
def main(directory):
# Get stats for files in directory
stats_dict = get_stats(directory)
...
I have a bunch of imports in this file. When I run this file, (the parameter is a string path to the directory), I get the following error
File "C:\Users\user\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/user/Desktop/project/src/detector_main.py", line 12
from ../tools.viz_generator import add_viz
and my project structure looks like below:
I feel like these inputs are not consistent and could be more organized. I just wrote a bunch of scripts and important that way but I feel like there is a way to make this into a more consistent package where users can run it anywhere with import issues.
Any help?
It sounds like what you're looking for is a Python Package.
https://www.pythoncentral.io/how-to-create-a-python-package/
Working with Python packages is really simple. All you need to do is:
Create a directory and give it your package's name. Put your classes
in it. Create a init.py file in the directory That's all! In order
to create a Python package, it is very easy. The init.py file is
necessary because with this file, Python will know that this directory
is a Python package directory other than an ordinary directory (or
folder – whatever you want to call it). Anyway, it is in this file
where we'll write some import statements to import classes from our
brand new package.
What I do is put my packages in
PYTHON_PATH\Lib\site-packages\MYPACKAGE
then in the __init__.py
from .function_scripts import *
My project path is like:
main.py
modules/
__init__.py
databaseManager.py
sync.py
excel.py
in main.py:
from modules.databaseManger import addExcelToDb, searchInDbAll
from modules.excel import search, showExcelDirContents
from modules.sync import syncExcelAndDB
and for example in database.py :
from modules.excel import showExcelDirContents
from modules.sync import insertExcelNameToSyncDb
but when I run main.py I get this error:
Traceback (most recent call last):
File "main.py", line 6, in <module>
from modules.databaseManger import searchIn
ImportError: cannot import name 'searchInDbAll'
and also having error when trying to import a function from each file in modules directory to others.
I need some examples of importing.
This is circular import issue.
Explanation:
You start by triggering import of databaseManager module.
During this databaseManager code starts to import excel.
During excel importing, excel code tries to retrieve function searchInDbAll() from databaseManager. But at that moment this function does not exist - because databaseManager is in process of importing excel and he hasn't started defining any functions.
How to fix:
In modules where circular import conflicts exist, import modules instead of functions. For example, change this:
from modules.excel import showExcelDirContents
to that:
from modules import excel
And of course, you must then change corresponding function calls, from showExcelDirContents() to excel.showExcelDirContents().
You must do this in your databaseManger, excel and sync modules. With this fix, I actually could run your code.
And yeah, remove appends to sys.path, that is wrong
You can append to your path where you put your modules like this:
import sys
sys.path.append('modules/')
or
import sys
sys.path.append('c:/mycode/stuff/modules/')
note those are forward slashes, or you can use double backslashes like \\
Then just have your databaseManger.py file in /modules
You'll also need to have a file in the /modules folder named:
__init__.py
Which is just an empty file
Then you should be able to do:
from databaseManger import addExcelToDb, searchInDbAll
I have 2 projects in the different directories, one of them I want to import to another one. Say, the project I want to import has this:
/path123/my_project/main_folder/file1.py
/path123/my_project/main_folder/file2.py
/path123/my_project/main_folder/file3.py
Here's what I did in the 2nd project:
import sys
sys.path.append('/path123/my_project/main_folder')
# it's indeed inserted
import main_folder.file1 # error - not found
from main_folder import file1 # error - not found
import my_project.main_folder.file1 # error - not found
After you appended the path where your second python file that you want to use in your first python file is you directly import the module by its filename without the extension. For example
import file1
Every location in your sys.path is then looked for the file file1.py to import.
Say you have your main python program in /prog1/main.py and you want to import the file from /prog2/lib/want_to_import.py in your main.py it should look like
import sys
sys.path.append('/prog2/lib')
import want_to_import