Search code examples
anacondacondavirtualenvpython-importpythonpath

Unable to add folders to PYTHONPATH when creating conda environment


I have a script that is making a conda virtual environment where functions need to be imported from folders outside of the repo. I have tried two approaches to add the folders to the path:

  1. Conda develop:

    os.system(conda run -n test_env conda develop ' + PACKAGE_PATH)

Results in:

Permission denied [PACKAGE_PATH]

  1. Create conda.pth manually

    path = os.path.join(env_path, 'lib') if not os.path.exists(path): os.mkdir(path) path = os.path.join(path, 'site-packages') if not os.path.exists(path): os.mkdir(path) env_path_file = os.path.join(path, 'conda.pth')

    with open(env_path_file, 'w+') as f: write_line(f, PACKAGE_PATH)

The file is created successfully. But, setting the conda interpreter from test_env (in Pycharm) and doing import sys; sys.path does not show [PACKAGE_PATH] and functions cannot be imported from its folders.

Any suggestions? Thank you.


Solution

  • This is resolved by using conda develop from outside the environment:

    conda develop [package_path] 
    

    not

    conda run -n conda develop [package_path]