# How use python functions from my python file in Julia?

**URL:** <https://discourse.julialang.org/t/how-use-python-functions-from-my-python-file-in-julia/79977>\
**Category:** General Usage\
**Created:** [April 25, 2022, 8:31am UTC](https://discourse.julialang.org/t/how-use-python-functions-from-my-python-file-in-julia/79977 "2022-04-25T08:31:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![YitaoCai](https://avatars.discourse-cdn.com/v4/letter/y/6bbea6/32.png) [@YitaoCai](https://discourse.julialang.org/u/YitaoCai)\
**Post date:** [April 25, 2022, 8:31am UTC](https://discourse.julialang.org/t/how-use-python-functions-from-my-python-file-in-julia/79977/1 "2022-04-25T08:31:24Z")

</div>

In python we do like

```julia
from mypython_folder.mypython_script import mypython_function/mypython_class

```

how do we use PythonCall.jl to do that?  
I tried

```julia
mypython_function = pyimport("mypython_folder.mypython_script" => "mypython_function")

```

it does not work

```julia
Python: ModuleNotFoundError: No module named 'mypython_folder.mypython_script'

```

after import the sys

```julia
@py import sys
sys.path

Python list: ['~/.julia/packages/PythonCall/XgP8G/python', '~/.julia/environments/v1.8/.CondaPkg/env/lib/python310.zip', '~/.julia/environments/v1.8/.CondaPkg/env/lib/python3.10', '~/.julia/environments/v1.8/.CondaPkg/env/lib/python3.10/lib-dynload', '~/.julia/environments/v1.8/.CondaPkg/env/lib/python3.10/site-packages', 
 '~/百度云同步盘/StudyResearch/AIforLS/Dermatology/code'] # this is the folder/module where all the python files located

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 25, 2022, 11:41am UTC](https://discourse.julialang.org/t/how-use-python-functions-from-my-python-file-in-julia/79977/2 "2022-04-25T11:41:03Z")

</div>

> [@YitaoCai](#):
>
> In python we do like

In the `python` interpreter, the current directory `.` is in the path by default. When calling from Julia you need to add the directory to `sys.path` explicitly.

> [@How to load and call functions defined in a standalone Python script?](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/2):
>
> Just do what you would have done in python julia\> using PyCall julia\> push!(pyimport("sys")["path"], pwd()); julia\> pyimport("a")[:foo]() Hello world! 3

and (I think) similarly for PythonCall where you would do

```julia
pyimport("sys").path.append(pwd())

```

(And, of course, you need to do this in `python` as as well as soon as you want to run code that is not in the current directory or the regular path.)

---

<div class="post-metadata">

**Author:** ![YitaoCai](https://avatars.discourse-cdn.com/v4/letter/y/6bbea6/32.png) [@YitaoCai](https://discourse.julialang.org/u/YitaoCai)\
**Post date:** [April 25, 2022, 12:14pm UTC](https://discourse.julialang.org/t/how-use-python-functions-from-my-python-file-in-julia/79977/3 "2022-04-25T12:14:52Z")

</div>

Thank you for your informative answer,  
yeah, I also tried to

```julia
@py import sys
sys.path.append(mypythonfolder) 
sys.path
Python list: ['~/.julia/packages/PythonCall/XgP8G/python', 
'~/.julia/environments/v1.8/.CondaPkg/env/lib/python310.zip', '~/.julia/environments/v1.8/.CondaPkg/env/lib/python3.10', '~/.julia/environments/v1.8/.CondaPkg/env/lib/python3.10/lib-dynload', '~/.julia/environments/v1.8/.CondaPkg/env/lib/python3.10/site-packages', 
 '~/百度云同步盘/StudyResearch/AIforLS/Dermatology/code'] <=# this is the folder/module where all the python files located**

```

but when I tried to access the pythonscript.py in that folder  
it will spit:

```julia
@py import code
code.anonymize_mrxs
ERROR: Python: AttributeError: module 'code' has no attribute 'anonymize_mrxs'

```
