# How to import a file in PythonCall

**URL:** <https://discourse.julialang.org/t/how-to-import-a-file-in-pythoncall/128565>\
**Category:** General Usage\
**Tags:** python, pythoncall\
**Created:** [April 30, 2025, 9:39am UTC](https://discourse.julialang.org/t/how-to-import-a-file-in-pythoncall/128565 "2025-04-30T09:39:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [April 30, 2025, 9:39am UTC](https://discourse.julialang.org/t/how-to-import-a-file-in-pythoncall/128565/1 "2025-04-30T09:39:56Z")

</div>

I have some python code I’d like to call in Julia. The code is in a file in the same folder as `pwd()` but no matter what method I use I always get the error `ERROR: Python: ModuleNotFoundError: No module named 'x'`

The first thing I did was

```julia
@pyexec """
from x import x
""" => x
ERROR: Python: ModuleNotFoundError: No module named 'x'

```

This is being run in a folder with a file named x.py. This line works when run in actual python.

The only partial success I’ve had is with

```julia
@pyexec """
import os
os.system("python x.py")
"""

```

Which runs just fine but I can’t get the class named `x` out.

What do I do?

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [April 30, 2025, 10:25am UTC](https://discourse.julialang.org/t/how-to-import-a-file-in-pythoncall/128565/2 "2025-04-30T10:25:11Z")

</div>

This works in Python because it includes `.` (the current folder) in `sys.path` by default when running interactively.

In PythonCall, Python is loaded in non-interactive mode (even if Julia itself is interactive) so this doesn’t happen so you can’t by default load Python files in the current directory.

A solution is to add this entry yourself, such as with

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

```

I suppose PythonCall could be changed so that Python is more similar to an interactive session when Julia is being used interactively.

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [April 30, 2025, 10:54am UTC](https://discourse.julialang.org/t/how-to-import-a-file-in-pythoncall/128565/3 "2025-04-30T10:54:14Z")

</div>

Thanks a lot!  
Is this mentioned in the PythonCall docs anywhere? Seems like a really unintuitive limitation for Python amateurs to figure out.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [April 30, 2025, 2:22pm UTC](https://discourse.julialang.org/t/how-to-import-a-file-in-pythoncall/128565/4 "2025-04-30T14:22:41Z")

</div>

Hm no I don’t think it’s in the docs - feel free to open an issue on GitHub linking to this thread.
