2

I have a silly question, but I haven't found any mention of it so far. I created a .py file in Python containing all of my functions that I am using for a job. Whenever I need to use them in a script, I have to address the path to the folder where the .py file is located, like the script below.

import os
os.chdir('...\\path-to-my-file')
import my-file as mfl

My question is: is there any way I can save the .py file with my functions right at the root of Anaconda and call it the same way I call Numpy, for example? I know that the libraries are in the 'C:\Users\User\anaconda3\Lib' folder, so I could save directly to that folder and call it in a more simplified way, like:

import numpy as np
import my-file as mfl

If this is feasible, would there be a standardized way to write the code?

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Lucas Oliveira
  • 197
  • 1
  • 7
  • You can create a package and install your code in a conda environment on your machine. Basically, you need to create a `setup.py` file using the [`distutils`](https://docs.python.org/3/library/distutils.html) module. – Chris Dec 05 '20 at 15:04
  • Why `setup.py` when you just want to call the function locally? This file should be used for packaging your code and likewise. – Mr. Hobo Dec 05 '20 at 16:00

2 Answers2

2

In order to be able to import mypackage the same way you do with any other module, the correct approach is to use pip locally:

python -m pip install -e /path_to_package/mypackage/

  • python -m ensures you are using the pip package from the same python installation you are currently using.

  • -e makes it editable, i/e import mypackage will reload after you make some changes, instead of using the cached one.

mypackage must be an installable package, i/e contain an __init__.py file, and a basic setup.py (or pyproject.toml file for pipenv)

minimal setup.py

from setuptools import find_packages, setup

setup(
    name='mypackage',          # Required
    version='0.0.1',           # Required
    packages=find_packages(),  # Required
)

the package structure must be like this:

mypackage/
    setup.py
    mypackage/    <----- this is a folder inside the other `mypackage/` folder
        __init__.py

or as a tree:

└── python_perso                folder
    └── mypackage                   folder
        ├── mypackage                   folder
        │   └── __init__.py
        └── setup.py

[edit] after installation, the directory will look like this:
(for a package named mypackage)

└── python_perso
    └── mypackage
        ├── mypackage
        │   ├── __init__.py
        │   └── __pycache__
        │       └── __init__.cpython-38.pyc
        ├── mypackage.egg-info
        │   ├── PKG-INFO
        │   ├── SOURCES.txt
        │   ├── dependency_links.txt
        │   └── top_level.txt
        └── setup.py

5 directories, 7 files
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Thank you! I have some questions. 1) You told me to use the pip install in "mypackage". This "mypackage" needs to be a folder with the 3 files __init__.py, setup.py and my-file.py (my functions)? 2) Does the setup.py file need to contain only the code you wrote? Don't my functions need to be in it? 3) Could you detail the __init__.py file? Does it need to contain my functions? 4) For the package structure you described, I need to have a "mypackage" folder containing the setup.py file and another folder called "mypackage" containing the __init__.py file and the file containing the my functions? – Lucas Oliveira Dec 06 '20 at 13:53
  • `setup.py` as described - `__init__.py` can be empty, or contain your functions; If it is empty, you can add a `myfunction.py` file that does contain your functions. Try it. – Reblochon Masque Dec 06 '20 at 14:54
  • I tried and it didn't work. At the command prompt I received the message `Successfully installed hello` and the folder with the files `setup.py`,` __init__.py` and `my-file.py` received a new folder called `hello.egg-info`. However, when I try to import `hello` or `my-file` into my script, it gives an error. Should I rename `hello` in the setup.py file to `my-file`? And should I save that folder right in the Anaconda library? – Lucas Oliveira Dec 06 '20 at 17:19
  • Please follow the instructions precisely: there is NO folder with the files `setup.py,` `init.py` and `my-file.py`! There is a folder `mypackage/` containing 2 items: a file `setup.py` and another folder `mypackage`. That last one contains `__init__.py`, and other `module.py` files that you may have. – Reblochon Masque Dec 06 '20 at 23:42
  • This folder structure was exactly what I created. However, it did not work despite the success of the installation. Nothing has been added to the Anaconda library and the `import` command does not work, either for calling `hello` or for `my-file`. The `hello.egg-info` folder was created inside the `mypackage` folder (the one that contains the `setup.py` file), which was inside `C:\Users\User`. Should I have created the `mypackage` folder inside the Anaconda library? And should the name `hello` in `setup.py` be the name of the file with my functions? – Lucas Oliveira Dec 07 '20 at 12:36
  • This is not the folder structure you described in your earlier comment: *the folder with the files setup.py,` init.py` and my-file.py received a new folder called hello.egg-info* - best to do `python -m pip uninstall mypackage`, delete the folders (keep your package), and start afresh. Nothing gets installed in your anaconda lib, unless you deliberately create the package there, and specify the path to it (I do not suggest you do that - better keep your personal installed folders in their own place) – Reblochon Masque Dec 07 '20 at 12:43
  • Okay, I finally got it. I did the following: the name `hello` in` setup.py` must be the same as the name of the folder next to the file `setup.py`. I used only the `__init __.py` file containing my functions. Now everything is running smoothly. Thanks a lot for the help! – Lucas Oliveira Dec 07 '20 at 13:30
0

I would suggest you create an Environment Variable called PYTHONPATH which will point you to additional functions. It is better to leave your anaconda root as it is if you are unsure of what you are doing. More on this here.

Mr. Hobo
  • 530
  • 1
  • 7
  • 22