Python sys.path.append - python

I am doing some multiprocessing work on python which isn't doable from a jupyter notebook. I have written the required functions in a .py file and I'm now importing it into my notebook.
How do I import this directly from the same folder without specifying the directory of where the file is in? This needs to be run-able on different PCs when sent to other people and thus I dont want to append the system path for my computer specifically.
import sys
#sys.path.append to get
sys.path.append()
from map_reducer import *
Thank you.

You can import them with:
from FileNameWithOutTheDotPy import *
And then just run them like normal:
FunctionThatDoesStuff(var1,var2)
Note: They do need to be in the same directory for this to work.

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.

unable to import "numpy" or pandas using python3 or python commandline, but able to import the packages in jupyter notebook

In jupyter notebook server, importing numpy or pandas is okay.
pic
However, by using python3 or python in commandline, I am not able to import anything because of a package named SystemRandom. pic2
Can anyone give me some suggestions?
Much Appreciated!
You have a file random.py on your Users folder ("/Users/chenyao/random.py") this file is interfering with the numpy import. You have two options:
rename the file
change directory
It tries to import SystemRandom from /Users/chenyao/random.py. Remove or rename that file and it should work.

Import Self Created Python Modules to Jupyter Notebook

I am trying to bring in a .py file that I have created and hosted in my Jupyter environment to a notebook. I have tried the following method, but it gives me an error.
import sys
sys.path.insert(0, '/path/to/file.py')
import file.py
I have several functions saved in this file that I want to be able to call, but when I run the above I get an error that the file.py was not able to be loaded in. Is there a special way within notebooks to load in created .py files?

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!

Import function of another file

I try to import my function Shemptoo from the file housoto.py:
from housoto import Shemptoo
ModuleNotFoundError: No module named 'housoto'
This two file are on the same directories, i am on spyder python 2.7 and window 10.
EDIT: Pranay, I tried on the console :
C:\Users\Pat>export
PYTHONPATH=//:$PYTHONPATH
Result:Access Denied.
I tried to keep init.py file in the same directory and it doesn't work to.
Also, if it can help you to find the problem, when i tried to just import the module Shemptoo in the file housoto with import, the problem is the same.
It's important for me to to that beacause my project contains thousands line..Thanks for your help.
One way could be adding the location of modules implicitly to the pythonpath:
export PYTHONPATH=/<location of module files>/:$PYTHONPATH
(to add python path for module)
Try import housoto directly. It may be because from tells the interpreter to look inside a folder (module which has an __init__.py file).
I did not know you were using Windows. For windows, you might want to look at this question and answers: How to add to the pythonpath in windows 7?

Categories

Resources