1

I am running macOS Mojave and I have installed Python 3.7 following this blog post here.

In short, what I did:

  1. Installed Xcode
  2. Set up Homebrew
  3. Installed Python 3
  4. Created a virtual environment

All following the steps in the article.

I now created a python file with some functions. I know that to use this file in a jupyter notebook as import my_file, I need to have the path to this file added in a PYTHONPATH usually done in the .bash_profile. Even though I did added export PYTHONPATH="/path/to/my/files/folder/:$PYTHONPATH", nothing happened.

My question is: How can I add a custom PYTHONPATH, so that I can import the file in jupyter or ipython?

Newskooler
  • 3,973
  • 7
  • 46
  • 84

3 Answers3

0

you forgot a $

export PYTHONPATH=$PYTHONPATH:/your/path/to/your/module
Astrom
  • 767
  • 5
  • 20
  • Should this be in the `.bash_profile`? If yes, does this affect only my virtual environment or all of them? Final question, why do I need to add `$PYTHONPATH`? – Newskooler Jan 21 '19 at 02:32
  • I guess for a virtualenv you might have a look at this: https://stackoverflow.com/a/17963979/7692655 – Astrom Jan 21 '19 at 02:35
  • do you source your bashrc? with 'source .bash_profile'? – Astrom Jan 21 '19 at 03:30
0

I have the same env too you, and there are some ways to solve it.

First, you can edit the sys.path using the sys.path.append() --Not recommended

import sys

import sys.path.append('/your/work/path')

Second, you can edit PYTHONPATH in "~/.bash_profile", but it will not work when you use the IDEA, such as Pycharm.

export PYTHONPATH=$PYTHONPATH:/your/work/path

source ~/.bash_profile

Last,you can just copy your work directory to dist-packages which you can find it using sys.path.

Also, you can find some other ways from Add to python path mac os x

Community
  • 1
  • 1
KyChen
  • 59
  • 4
  • Why do you use `sys.path` afterwards? – Newskooler Jan 21 '19 at 02:42
  • check the config whether works . it will print all the directory – KyChen Jan 21 '19 at 02:45
  • It does not work. Also it makes no sense to edit `PYTHONPATH` and then use `sys.path`. You can see here for reference: https://stackoverflow.com/questions/3387695/add-to-python-path-mac-os-x – Newskooler Jan 21 '19 at 03:28
  • I check the ipython/python/jupyter in terminal, actually it does works. But while i use the IDE(pycharm), it seems doesn't get the PYTHONPATH – KyChen Jan 21 '19 at 03:52
0

In my installation, /usr/local/bin/ipython was a script containing

#!/bin/bash
PYTHONPATH="/usr/local/Cellar/ipython/6.5.0/libexec/lib/python3.7/site-packages:/usr/local/Cellar/ipython/6.5.0/libexec/vendor/lib/python3.7/site-packages" exec "/usr/local/Cellar/ipython/6.5.0/libexec/bin/ipython" "$@"`.

Changing this to

#!/bin/bash
PYTHONPATH=$PYTHONPATH:"/usr/local/Cellar/ipython/6.5.0/libexec/lib/python3.7/site-packages:/usr/local/Cellar/ipython/6.5.0/libexec/vendor/lib/python3.7/site-packages" exec "/usr/local/Cellar/ipython/6.5.0/libexec/bin/ipython" "$@"

did the trick for me.

user2582713
  • 115
  • 6