简体   繁体   中英

Import own .py files in anaconda spyder

I've written my own mail.py module in spider (anaconda). I want to import this py file in other python (spider) files just by 'import mail'

I searched on the internet and couldn't find a clearly solution.

To import any python script, it should exist in the PYTHONPATH. You can check this with the following code:

import sys
print sys.path

To import your Python script:

  1. Put both the scripts (main and the imported python script) in the same directory.
  2. Add the location of the file to be imported to the sys.path.

For example, if the script is located as '/location/to/file/script.py':

 import sys
 sys.path.append('/location/to/file/')
 import script

I had the same problem, my files were in same folder, yet it was throwing an error while importing the "to_be_imported_file.py".

I had to run the "to_be_imported_file.py" seperately before importing it to another file.

I hope it works for you too.

There are many options, eg

  • Place the mail.py file alongside the other python files (this works because the current working dir is on the PYTHONPATH.
  • Save a copy of mail.py in the anaconda python environment's "/lib/site-packages" folder so it will be available for any python script using that environment.

I did a slightly different solution approach that is less sophisticated. When I start my anaconda terminal it is at a C prompt. I just did a cd d:\\mypython\\lib in the beginning window before starting python. once I did that I could simply just import my own classes that I put in that library with "import MyClass as my" then I was off and running. It is interesting, I did 2 days of internet searching in my part time and could not find the answer either, until I asked a friend.

cd d:\mypython\lib
python
>>> import MyClass as my
>>> my1=my.MyClass()
>>> my1.doSomething() 

worked for me on my anaconda / windows 10 environment python 3.6.6.

从另一个文件调用任何函数时,应注意不要在函数内部导入任何库

Searched for an answer for this question to. To use a .py file as a import module from your main folder, you need to place both files in one folder or append a path to the location. If you storage both files in one folder, then check the working directory in the upper right corner of the spyder interface. Because of the wrong working directory you will see a ModuleNotFoundError .

I believe the easiest solution is to place the directory containing your Python files into the Anaconda site-packages folder on your machine. I wrote an article outlining the whole process but, in short, you'll need to create a folder containing your python script and an __init__.py file. Then place that folder inside the site-packages folder in the Anaconda directory.

On Windows the site-packages directory is typically located at:
C:\\Users\\[your_username]\\Anaconda3\\Lib\\site-packages\\

On Mac the site-packages directory is typically located located at: Users/[your_username]/opt/anaconda3/lib/[python3.8]/site-packages/

Notice, on Mac the Python version matters. You'll need to look for the directory that corresponds to the (base) Python version used by Anaconda. Also, anything I've placed inside of square brackets in the file paths above need to be changed according to your particular machine and Python version.

The file structure should look something like this:

~/
 |__site-packages/
    |__your_folder/
        script.py
        __init__.py

After you have the folder containing your script.py and __init__.py file moved into the site-packages subdirectory of Anaconda, you'll be able to import it from any script you run on your machine .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM