Cannot import modules from different folders - python

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

Related

How do I properly run a separate python script from main python file?

I am trying to make my python script more modular -- it works properly when everything is in just one lengthy .py file, but I want to use the main python file to call other files to streamline the flow and make upgrades easier.
I'm struggling with package imports. One package I'm using is os, which I import in the main file:
import os
import pandas as pd
import numpy as np
from code_module_1 import *
if __name__ == '__main__':
code_module_1()
I also import it at the top of the python file that is called, code_module_1.py:
import os
import glob
import pandas as pd
import numpy as np
def function_called_from_main():
for root, dirs, files in os.walk('...file_path'):
etc.
When I do this, I receive an error saying the name 'os' is not defined, which I've taken to mean that the package isn't being imported in code_module_1.
I've attempted to fix this by placing the lines of code that import packages inside of the function that I'm calling from the main script, but I still run into the same error. Where should I be importing packages, and how do I make sure that other python files that are called have the packages that they need to run?
With the following line
from code_module_1 import *
you import everything defined in the module. So basically, all function definitions are available.
Next, you can't execute a module. You can only execute a function or a statement. Thus, the following line
if __name__ == '__main__':
code_module_1()
should be something like this:
if __name__ == '__main__':
function_called_from_main()
where you execute the function function_called_from_main that was previously imported.

Python: import files from parent dirs or relative path

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

How to import a library in a script that is being called by another script in Python?

I have a python file called main.py which look like this:
#!/usr/bin/python
import numpy as np
import load
a = load.compute()
print(a)
The above file calls a module called load.py which looks like this:
def compute():
import numpy as np
a = np.array([2,3,1,0])
return a
However, I get the error: NameError: name 'np' is not defined.
I have also tried to put import numpy as np as the first line of load.py before the compute() function, and still received the same error.
How can I import a library in a script that is being called by another Python script?
You don't need to import numpy in main.py,
but anyway: your code works. Check if files in same directory, try to call python main.py from that directory.
The error you have mentioned is not reproducible. It works for me.

Importing library once for a python module

I want to structure imports of my python module/package, namely I have several *.py files in my module. All of them use:
import numpy as np
in some pf them I use:
import pandas as pd
can I set the global import for my python module, and say that it uses numpy as np in all the *.py files of the module.
I tried something in __init__.py but it didn't work as expected. Is it anyhow reasonable to make global imports?
No, you cannot do this, it is fundamentally opposed to the way Python works.
Warning:- Do not use this at home.Not a good practice
You can Do it by importing all your libraries into a single file. For example:-
library.py
import numpy as np
import os
import json
import pandas as pd
And then import this file in your code files
main.py
from library import *
a = np.array([1, 2, 3])

Unable to import a local project to another local one by absolute path:

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

Categories

Resources