how to manage sys.path globally in pycharm

2020-07-06 07:21发布

问题:

I have a bunch of unittests that import stuff not from /python2.7/site-packages/ but from a completely different directory. So what I do is do some monkey-patching in each test file with sys.path.append to make my Python see what I need.

Is there a way to append sys.path globally for the whole Pycharm project without bothering with each and every file?

UPDATE: setting default working directory doesn't help. For example, if I set the working directory to /Users/1111/_projects/_empty_dir/ like so:

and then run this

import sys
for p in sys.path: print p

what I see is this

/Users/1111/.virtualenvs/blesk/bin/python /Users/1111/_projects/_testing_pycharm/importer.py
/Users/1111/_projects/_testing_pycharm
/Users/1111/.virtualenvs/blesk/lib/python2.7/site-packages/six-1.9.0-py2.7.egg
/Users/1111/.virtualenvs/blesk/lib/python2.7/site-packages/parse-1.6.6-py2.7.egg
/Users/1111/_projects/_testing_pycharm
/Users/1111/.virtualenvs/blesk/lib/python27.zip
/Users/1111/.virtualenvs/blesk/lib/python2.7
/Users/1111/.virtualenvs/blesk/lib/python2.7/plat-darwin
/Users/1111/.virtualenvs/blesk/lib/python2.7/plat-mac
/Users/1111/.virtualenvs/blesk/lib/python2.7/plat-mac/lib-scriptpackages
/Users/1111/.virtualenvs/blesk/lib/python2.7/lib-tk
/Users/1111/.virtualenvs/blesk/lib/python2.7/lib-old
/Users/1111/.virtualenvs/blesk/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/1111/.virtualenvs/blesk/lib/python2.7/site-packages

Process finished with exit code 0

and there is no /Users/1111/_projects/_empty_dir/ on sys.path.

回答1:

This is one fairly easy, follow this path:

  1. settings
  2. project: <your project name>
  3. project interpreter
  4. select the interpreter you want to update sys.path for
  5. find the bottom right icon, 'show paths for the selected interpreter'
  6. add the folder to the list of paths.


回答2:

Yes.

I am on a Mac, using PyCharm 4.5.1 Professional Edition. Your platform and version may be a bit different, but if you make sure you are on the latest version, it should work.

EDIT: I've updated this answer and uploaded a new screenshot.

What you want is to set the working directory and the PYTHONPATH environment variable.

The working directory sets the directory to be used by the running task. and the PYTHONPATH environment variable adds directories to sys.path

Go to Run -> Edit Configurations

In the dialog box that comes up, navigate to Defaults -> Python tests -> Unittests

Set your working directory and your PYTHONPATH environment variable and you are all set. Your tests will be automatically run from that working directory and have the sys.path that you want. You can include multiple paths in your PYTHONPATH environment variable if you separate them with a colon :

For example:

PYTHONPATH=/path/to/dir:/path/to/other/dir

If you already have some existing test configurations, you may want to delete them so that they get re-created when you run tests or set the working directory and PYTHONPATH for each of them. You can delete them from the same dialog window

Run -> Edit Configurations then expand Python Tests to see your existing test configurations

If you don't want to delete your existing test configurations, just specify the working directory for each existing test configuration individually.



回答3:

There is now a better way to do it. You can Mark Directory as as Sources root. This option will appear in the bottom when you right click on any directory in Project.