How to Add Python Path in Ubuntu 16.04

I'm trying to run a scrapy project on a ubuntu server. For which I need to add the project path to python path.

I created a .bash_profile file in the /home directory with the following contents:

PYTHONPATH=$PYTHONPATH:/home/john/Desktop/myscraper/
EXPORT $PYTHONPATH

But I'm getting error running my python file stating it didn't find the module.

ImportError: No module named myscraper.items

I tried using the following paths, but nothing works.

  • /home/john/Desktop/myscraper/
  • /home/john/Desktop/myscraper
  • home/john/Desktop/myscraper/
  • home/john/Desktop/myscraper
2

2 Answers

In addition to NeoTheThird's answer:

Ubuntu does not use ~/.bash_profile by default. You should use ~/.profile instead.

The path you should use is /home/john/Desktop/myscraper, though /home/john/Desktop/myscraper/ would also work. Paths that don't start with slashes are relative, not absolute, so will not work unless the working directory is /. More details here on Wikipedia.

You can put the definition and export statements together, and if PYTHONPATH is not already defined, you can leave off the $PYTHONPATH: at the start.

export PYTHONPATH=/home/john/Desktop/myscraper
1

Config files belong in your personal home directory (/home/$USER, $HOME or simply ~), not in the /home directory. In your case that will be /home/john.

Please also make sure to use the correct casing, it's export in all lowercase.

Since export is not accessing but referencing the variable, you do not use the $ sign: export PYTHONPATH

Are you sure you want to have this in your .bash_profile and not your .bashrc? You can read up on the difference here.

In any case you will have to run source ~./bash_profile (or source ~./bashrc if you go with that) for your changes to take effect.

6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest